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

白昼怎懂夜的黑 提交于 2020-11-30 00:40:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!