Let's say I have 2 collections which both have a phone
attribute:
$contacts = Contact::all();
$optouts = Optout::all();
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
.
How do I do it?
$contacts = Contact::all();
$optouts = Optout::all()->pluck('phone');
$filtered = $contacts->whereNotIn('phone', $optouts);
More info here: https://laravel.com/docs/5.4/collections
one option would be:
$phones = $optouts->pluck('phone')->toArray();
$newContacts = $contacts->reject(function ($contact) use ($phones) {
return in_array($contact->phone, $phones);
});