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

April 22, 2018 —John Koster

The whereNotInStrict method will return all of the items in a collection that do not have matching values (supplied via the $values parameter) for the provided $key. This method is capable of filtering collections of both arrays and objects. The whereNotInStrict behaves similarly to the whereNotIn method, the difference is that the whereNotInStrict method always performs strict comparisons.

The whereNotInStrict method does not modify the original collection.

#Signature

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

#Example Use

The following examples are behaviorally equivalent:

1$users = collect([
2 [
3 'username' => 'Alice',
4 'isBanned' => true
5 ],
6 [
7 'username' => 'Bob',
8 'isBanned' => 'true'
9 ],
10 [
11 'username' => 'Charlie',
12 'isBanned' => 1
13 ]
14]);
15 
16// Using the whereNotIn method and $strict set to true.
17$validUsers = $users->whereNotIn('isBanned', [
18 true
19], true);
20 
21// Using the whereNotInStrict method.
22$validUsers = $users->whereNotInStrict('isBanned', [
23 true
24]);

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.