Laravel 5 Collections: Filtering a Collection Based On Key Presence Using Strict Comparison Operators With whereInStrict

April 22, 2018 —John Koster

The whereInStrict method is similar to the whereIn method; the difference is that the whereInStrict method will always use strict comparisons where determining the final collection results.

The whereInStrict method does not modify the original collection; it will return a new collection instance.

#Signature

1public function whereInStrict(
2 $key,
3 $values
4);

#Example Use

The $people collection will be used in the following example:

1$people = collect([
2 ['name' => 'Alice', 'age' => 26],
3 ['name' => 'Bob', 'age' => '26'],
4 ['name' => 'Chris', 'age' => 26]
5]);

The second person, Bob, has the same age as the other two people; the difference is that is age is expressed as a string rather than an integer.

The following method calls are functionally equivalent:

1// Using the whereIn method.
2$twentySix = $people->whereIn('age', [26], true);
3 
4$twentySix = $people->whereInStrict('age', [26]);

In both cases, the returned collection would contain two elements; they would contain entries for both Alice and Chris, since their age was expressed as integers.

Some absolutely amazing
people

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.