PHP code example of michiruf / laravel-collection-differ

1. Go to this page and download the library: Download michiruf/laravel-collection-differ library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

michiruf / laravel-collection-differ example snippets


$result = collect([1, 2, 3])->differ([2, 4])->diff();

dump($result->unmatchedSource); // [1, 3]
dump($result->unmatchedDestination); // [4]
dump($result->matched); // [[2, 2]]

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();

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();