Table of Contents
The method where()
allows users to filter collection items given a key-value pair. This method filters items by checking whether the $key
has some value equal to the provided $value
.
You can also pass a parameter to control how the method compares $key with $value. Laravel runs the given command and returns a new collection of instances containing filtered items that satisfy the condition.
Structure of where()
Illuminate\Support\Collection
class provides an easy, accessible wrapper for working with arrays of data.
/** * Filter items by the given key value pair. * * @param string $key * @param mixed $operator * @param mixed $value * @return static */ public function where($key, $operator = null, $value = null) { ... }
Examples
The following code displays the basic usage of where()
method:
<?php use Illuminate\Support\Collection; // Create a new collection. $collection = new Collection([ [ 'country' => 'America', 'name' => 'James', 'age' => '22' ], [ 'country' => 'America', 'name' => 'Jason', 'age' => '4' ], [ 'country' => 'Canada', 'name' => 'Jusitn', 'age' => '12' ] ]); // Get all people with country 'America' $people = $collection->where('country', 'America'); dd($people);
As a result, this will return a collection where each array is compared using the key country
to check whether the value is America
. Filtered results will hold values similar to the following output:
Illuminate\Support\Collection { #items: array [ 0 => array [ 'country' => 'America', 'name' => 'James', 'age' => '22' ] 1 => array [ 'country' => 'America', 'name' => 'Jason', 'age' => '4' ] ] }
In addition, you can also use the less than operator as a second argument to filter people having age less than 16.
$filtered = $collection->where('age', '<', 16); dd($filtered);
The result will be something like this:
Collection {#668 ▼ #items: array:2 [▼ 1 => array:2 [▼ "country" => "America", "name" => "Jason", "age" => 4 ] 2 => array:2 [▼ "country" => "Canada", "name" => "Jusitn", "age" => 12 ] ] }
Alternatively, you can also provide a comparison operator in the format ($key
, $operator
, $value)
like in the second example. In case you do not provide any operator, then it is set to an equal operator (=)
by default.
Other operators that can be used where()
are:
Operators | Description |
= | checks equality between $value and $key in collection. Does not compare value types |
== | Same as above |
=== | Same as = but also compares value types. |
<> | Ensures inequality between the check $value and the key value on the collection item. This operator compares the types of the values. |
!== | Make sure that the key value on the collection item is greater than the supplied check $value . |
< | Ensures that the key value on the collection item is less than the supplied check $value . |
< | Make sure that the key value on the collection item is greater than the supplied check $value . |
<= | Ensures that the key value on the collection item is less than or equal to the supplied check $value . |
>= | Ensures that the key value on the collection item is greater than or equal to the supplied check $value . |
Check more tutorials by clicking here. To know more about other methods that deal with collection items you can always get help from the documentation.