Kevin Eaton

Packages · Open source

Laravel Dedupe

Removes duplicates from a list of records and tells you why each one was removed. Every dropped record comes back with the record it matched and the key it matched on.

A buyer asks why a lead they paid for last month was thrown out as a duplicate. "Because unique() said so" is not an answer you can send.

Before

$signups = [
    ['id' => 1, 'name' => 'Jane Doe', 'email' => 'jane@example.com'],
    ['id' => 2, 'name' => 'Bob Ray',  'email' => 'bob@example.com'],
    ['id' => 3, 'name' => 'Jane Doe', 'email' => ' Jane@Example.com'],
    ['id' => 4, 'name' => 'J. Doe',   'email' => 'JANE@EXAMPLE.COM '],
];

$survivors = collect($signups)
    ->unique(fn (array $signup) => strtolower(trim($signup['email'])));

echo $survivors->pluck('id')->implode(', '), "\n";
1, 2

Those are the right two records. But 3 and 4 are gone, and nothing says which record each one matched, or on what. When someone asks where their signup went, you have no answer. Here is the same list with the package.

After

use Keaton\Dedupe\Deduper;
use Keaton\Dedupe\Normalizers\LowercaseTrim;

$result = Deduper::make()
    ->key('email', new LowercaseTrim)
    ->run($signups);

echo implode(', ', array_column($result->survivors, 'id')), "\n";

foreach ($result->audit as $drop) {
    echo "dropped {$drop->droppedId}: same {$drop->matchedKey} as {$drop->survivorId}\n";
}
1, 2
dropped 3: same email as 1
dropped 4: same email as 1

The same two records are kept, and each dropped record now says which one it matched and on which key. key() names the field to compare, and LowercaseTrim is the rule that cleans the value up first. In a Laravel app the trail can be stored in the database, so you can still answer that question a month later.

What it does

  • Clean values up before comparing them. There are rules for emails and phone numbers, and you can write your own.
  • Match on a combination of fields, like last name and zip code, when no single field identifies a record.
  • Set a time window, so two records only count as duplicates when they are close together in time.
  • Keep the first or the last record of each group.
  • Store the audit trail in the database, and read it back by run or by record.
  • Each entry holds a hash of the dropped record, so you can prove which record it was without keeping a copy of it.