Skip to content

Conversation

@techmahedy
Copy link
Member

This PR introduces a new repair() method to the Entity ORM query builder, designed to automatically fix, normalize, and improve data quality at scale.

The method allows developers to:

  1. Apply a normalization or repair callback to retrieved entities
  2. Optionally, persist only the records that were actually modified
  3. Preview changes without writing to the database

This provides a clean, expressive way to handle common data quality tasks such as formatting, normalization, and cleanup.

API Overview

/**
 * Get records and fix/normalize data
 *
 * @param callable $fixer
 * @param bool $saveChanges
 * @return Collection
 */
public function repair(callable $fixer, bool $saveChanges = false): Collection

Example: Fix & Persist Changes

User::query()
    ->where('role', 'admin')
    ->repair(function ($user) {
        $user->email = strtolower($user->email);
        $user->name  = ucwords($user->name);
    }, true); // true = save changes

Behavior:

  1. Retrieves all matching users
  2. Normalizes email and name
  3. Saves only users whose attributes actually changed

Example: Preview Changes (No Save)

$users = User::query()
    ->repair(function ($user) {
        $user->phone = preg_replace('/[^0-9]/', '', $user->phone);
    }, false);

Behavior:

  1. Returns a collection of repaired entities
  2. No database writes occur
  3. Useful for previews, audits, and dry runs

Use Cases

Data normalization after imports
One-time cleanup scripts

@techmahedy techmahedy merged commit 2812815 into doppar:3.x Jan 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant