November 30, 2016 —John Koster
whereInLoose($key, array $values)
The whereInLoose
method is similar to the whereIn
(discussed in the Laravel Collection Public API: whereIn article) method in that both methods can be used to filter the collection based on a given $key
and an array of possible $values
that the $key
can have. The whereLoose
(discussed in the Laravel Collection Public API: whereLoose article) method by default performs type checking to determine if an item is present in a collection (this can be modified by supplying a third argument to the whereIn
method). The whereInLoose
method exists as a shortcut to using the whereIn
method, removing the need of an optional third argument. This method does not change the original Collection
instance and will return a new, modified, Collection
instance.
The same sample will be used from the whereIn
method:
1<?php2 3$people = collect([4 ['name' => 'Alice', 'age' => 26],5 ['name' => 'Bob', 'age' => 34],6 ['name' => 'Chris', 'age' => 26]7]);
The following two method calls are identical in their results:
1$collectionA = $people->whereIn('age', ['26'], false);2$collectionB = $people->whereInLoose('age', ['26']);
∎
The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.