问题
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.
回答1:
Use Collection::diff():
$doorsWithAccess = $doors->diff($users_doors);
来源:https://stackoverflow.com/questions/53458183/how-do-i-compare-2-eloquent-collections-and-remove-whats-in-one-from-the-other