Finchy70
Finchy70

Reputation: 473

How do I compare 2 eloquent collections and remove whats in one from the other?

I have a collection of all doors, and a collection of doors that the current user has access to. How can I compare the two and remove from the all doors collection the doors the user already has access to?

$doors = Door::orderBy('name', 'asc')->get();
$users_doors = $user->doors;

Here are the two collections.

Upvotes: 0

Views: 1797

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

Use Collection::diff():

$doorsWithAccess = $doors->diff($users_doors);

Upvotes: 3

Related Questions