Remove from collection where exists in another collection

kjd*_*n84 2 php collections laravel

Let's say I have 2 collections which both have a phone attribute:

$contacts = Contact::all();
$optouts = Optout::all();
Run Code Online (Sandbox Code Playgroud)

I want to update the $contacts collection and remove all which are opted out. So I want to remove all $contacts whose phone is present in $optouts.

我该怎么做?

小智 5

$contacts = Contact::all();
$optouts = Optout::all()->pluck('phone');

$filtered = $contacts->whereNotIn('phone', $optouts);
Run Code Online (Sandbox Code Playgroud)

更多信息在这里: https: //laravel.com/docs/5.4/collections