i have collection
Illuminate\Support\Collection {#1453
#items: array:4 [
0 => "three"
1 => "nine"
2 => "one"
3 => "two"
]
}
and this string
'one', 'two', 'three'
i am trying to validate if these all strings available in array
$array->contains('one', 'two', 'three')
it should return true
but everytime i am getting false
what i am doing wrong please explain thank you
I use Collection:diff
in combination with Collection::isEmpty
for a reusable containsAll
macro. When the supplied values contain an element that's not included in the collection to check against the result of diff
won't be empty and therefore return false.
use Illuminate\Support\Collection;
Collection::macro('containsAll', function (...$values) {
return collect($values)->diff($this)->isEmpty();
});
$collection = collect(['three', 'nine', 'one', 'two']);
$collection->containsAll('one', 'two', 'three'); // true
$collection->containsAll('one', 'five', 'three'); // false
Thanks its working