Przejdź do treści

Laravel porównanie dwóch kolekcji

W zasadzie przykład banalny ale często się zapomina te proste metody utrudniając sobie tym życie. Często piszemy coś co jest już napisane i do tego w narzędziach ktore już to posiadają. To jak mawia mój Kolega Slava jak wymyślanie roweru na nowo. Wystarczy użyć.

Slava to rower dla Ciebie:)

Przykład z kolekcjami laravela

https://laravel.com/docs/8.x/collections#method-intersect

$oldCollection = collect(['Region1', 'Region2', 'Region3']);
$newCollection = collect(['Region2', 'Region4', 'Region7']);

$theSameElement = $newCollection->intersect($oldCollection)->all();
$newElement = $newCollection->diff($oldCollection)->all();
$removedElement = $oldCollection->diff($newCollection)->all();


dd(
    $theSameElement,
    $newElement,
    $removedElement
);
// Result
//        array:1 [▼
//          0 => "Region2"
//        ]
//        array:2 [▼
//          1 => "Region4"
//          2 => "Region7"
//        ]
//        array:2 [▼
//          0 => "Region1"
//          2 => "Region3"
//        ]

// theSameElement : is Not important because exist
// newElement : maybe must Add
// removed : maybe must Remove

// Can Use many method on collection

I przypadek z kolekcjami Eloquent

https://laravel.com/docs/8.x/eloquent-collections#method-intersect

$oldUsersCollection = User::select(['id','email'])->whereIn('id', [1, 2, 3])->get();
$newUsersCollection = User::select(['id','email'])->whereIn('id', [2, 3, 4])->get();
$theSameUsers = $newUsersCollection->intersect($oldUsersCollection)->toArray();
$newUsers = $newUsersCollection->diff($oldUsersCollection)->toArray();
$removedUsers = $oldUsersCollection->diff($newUsersCollection)->toArray();

dd(
    $theSameUsers,
    $newUsers,
    $removedUsers, 
);

I przykład z wybieraniem samych kluczy:

$oldUsersCollection = User::select(['id','email'])->whereIn('id', [1, 2, 3])->get();
$newUsersCollection = User::select(['id','email'])->whereIn('id', [2, 3, 4])->get();
$theSameUsers = $newUsersCollection->intersect($oldUsersCollection)->pluck('id','id')->toArray();
$newUsers = $newUsersCollection->diff($oldUsersCollection)->pluck('id','id')->toArray();
$removedUsers = $oldUsersCollection->diff($newUsersCollection)->pluck('id','id')->toArray();

dd(
    $theSameUsers,
    $newUsers,
    $removedUsers,
);

array:2 [3 => 3
  2 => 2
]
array:1 [4 => 4
]
array:1 [1 => 1
]

A na deser wszystkie obiekty razem w jednej tablicy, choć pytanie po co… 🙂

$oldUsersCollection = User::select(['id','email'])->whereIn('id', [1, 2, 3])->get();
$newUsersCollection = User::select(['id','email'])->whereIn('id', [2, 3, 4])->get();

$mergedCollection = $oldUsersCollection->merge($newUsersCollection)
                                        ->flatten(1)
                                        ->values()
                                        ->pluck('id','id')
                                        ->toArray();

dd($mergedCollection);

//        array:4 [▼
//            3 => 3
//            2 => 2
//            1 => 1
//            4 => 4
//        ]