Skip to content

Commit 91237ee

Browse files
authored
clarify where(), whereNull(), and whereNotNull() usage (#10814)
this change helps clarify usage of the `where()`, `whereNull()`, and `whereNotNull()` methods with a `null` value. discourage the use of `$collection->where('field', '!=', null)` because it will possibly cause unexpected results with "nully" values. show more values that are "nully" and how they will be treated with the strict check.
1 parent 969aa32 commit 91237ee

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

collections.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3745,25 +3745,25 @@ $filtered->all();
37453745
*/
37463746
```
37473747

3748-
The `where` method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the [whereStrict](#method-wherestrict) method to filter using "strict" comparisons.
3748+
The `where` method uses "loose" comparisons when checking item values, meaning a string with an integer value will be considered equal to an integer of the same value. Use the [whereStrict](#method-wherestrict) method to filter using "strict" comparisons, or the [whereNull](#method-wherenull) and [whereNotNull](#method-wherenotnull) methods to filter for `null` values.
37493749

37503750
Optionally, you may pass a comparison operator as the second parameter. Supported operators are: '===', '!==', '!=', '==', '=', '<>', '>', '<', '>=', and '<=':
37513751

37523752
```php
37533753
$collection = collect([
3754-
['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'],
3755-
['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'],
3756-
['name' => 'Sue', 'deleted_at' => null],
3754+
['name' => 'Jim', 'platform' => 'Mac'],
3755+
['name' => 'Sally', 'platform' => 'Mac'],
3756+
['name' => 'Sue', 'platform' => 'Linux'],
37573757
]);
37583758

3759-
$filtered = $collection->where('deleted_at', '!=', null);
3759+
$filtered = $collection->where('platform', '!=', 'Linux');
37603760

37613761
$filtered->all();
37623762

37633763
/*
37643764
[
3765-
['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'],
3766-
['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'],
3765+
['name' => 'Jim', 'platform' => 'Mac'],
3766+
['name' => 'Sally', 'platform' => 'Mac'],
37673767
]
37683768
*/
37693769
```
@@ -3922,6 +3922,11 @@ $collection = collect([
39223922
['name' => 'Desk'],
39233923
['name' => null],
39243924
['name' => 'Bookcase'],
3925+
['name' => 0],
3926+
['name' => 0.0],
3927+
['name' => ''],
3928+
['name' => []],
3929+
['name' => false],
39253930
]);
39263931

39273932
$filtered = $collection->whereNotNull('name');
@@ -3932,6 +3937,11 @@ $filtered->all();
39323937
[
39333938
['name' => 'Desk'],
39343939
['name' => 'Bookcase'],
3940+
['name' => 0],
3941+
['name' => 0.0],
3942+
['name' => ''],
3943+
['name' => []],
3944+
['name' => false],
39353945
]
39363946
*/
39373947
```
@@ -3946,6 +3956,11 @@ $collection = collect([
39463956
['name' => 'Desk'],
39473957
['name' => null],
39483958
['name' => 'Bookcase'],
3959+
['name' => 0],
3960+
['name' => 0.0],
3961+
['name' => ''],
3962+
['name' => []],
3963+
['name' => false],
39493964
]);
39503965

39513966
$filtered = $collection->whereNull('name');

0 commit comments

Comments
 (0)