michiruf/laravel-collection-differ is a Laravel package for tiny helper to diff collections.
It currently has 2 GitHub stars and 1.062 downloads on Packagist (latest version 0.4).
Install it with composer require michiruf/laravel-collection-differ.
Discover more Laravel packages by michiruf
or browse all Laravel packages to compare alternatives.
Last updated
Tiny helper to diff collections.
composer require michiruf/laravel-collection-differ
Result usage example:
$result = collect([1, 2, 3])->differ([2, 4])->diff();
dump($result->unmatchedSource); // [1, 3]
dump($result->unmatchedDestination); // [4]
dump($result->matched); // [[2, 2]]
Callbacks usage example:
ProductModel::all()
->differ(ProductApi::getAll())
->handleUnmatchedSourceUsing(fn (ProductModel $model) => $model->delete())
->handleUnmatchedDestinationUsing(fn (ProductDto) $dto => ProductModel::createFromDto($dto))
->handleMatchedUsing(fn (ProductModel $model, ProductDto $dto) => $model->updateWithDto($dto))
->validateUniqueness() // throw if identifiers are not unique
->diff();
Identifier usage example:
ProductModel::all()
->differ(ProductApi::getAll())
->identifySourceUsing(fn (ProductModel $model) => $model->id)
->identifyDestinationUsing('meta.id') // or: fn (ProductDto $dto) => $dto->meta->id
->validateUniqueness() // throw if identifiers are not unique
->diff();
For additional examples, have at look at the tests.