Laravel Collection – Filtering Collection Items using where()
Laravel Collections ( 11 Lessons )
Laravel Collections is one of the most powerful features of Laravel. Basically, collections are PHP arrays but it’s an Object Oriented approach to deal with PHP arrays.
see full series- Laravel Collections – Introduction to Collections
- Laravel Collections – make Method
- Laravel Collections – wrap() and unwrap() Methods
- Laravel Collections – map() Method
- Laravel Collections – times() Method
- Laravel Collections – collapse() Method
- Laravel Collections – contains() & containsStrict() Methods
- Laravel Collections – each() Method
- Laravel Collection – Using toJson() Method
- Laravel Collection – Filtering Collection Items using where()
- Laravel Collection – Using splice() Method to Remove Items
Laravel Collection provides the ability to filter a collection’s items by using a key value pair by using where()
method. The where()
method will check if the $key
has some value equal to the provided $value
. Also you can provide an extra argument $operator
parameter to control the comparison of the $key
and $value
. The where()
method return a new collection instance containing all the items which satisfy the given criteria.
Signature
The signature of where()
method in the Illuminate/Support/Collection class looks something like below.
/** * 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) { ... }
Laravel Collection Example
Let’s use the where method to filter out our below collection.
// Create a new collection $collection = collect([ [ 'name' => 'Luke Thomas', 'job' => 'Marketing Manager', 'salary' => 8000 ], [ 'name' => 'Liam Coleman', 'job' => 'Web Manager', 'salary' => 7500 ], [ 'name' => 'Jacob Hill', 'job' => 'Web Developer', 'salary' => 6500 ], [ 'name' => 'Charles Brown', 'job' => 'Web Developer', 'salary' => 5000 ], [ 'name' => 'Richard Wilson', 'job' => 'Web Developer', 'salary' => 5400 ], [ 'name' => 'Isaac Kelly', 'job' => 'Graphics Designer', 'salary' => 6000 ] ]);
For example, we want to extract all employees having a salary of over $6000. We can use the where()
method to filter our records like below.
$filteredCollection = $collection->where('salary', '>', 6000); dd($filteredCollection);
The above statement will return the response to something like below.
Collection {#668 ▼ #items: array:3 [▼ 0 => array:3 [▼ "name" => "Luke Thomas" "job" => "Marketing Manager" "salary" => 8000 ] 1 => array:3 [▼ "name" => "Liam Coleman" "job" => "Web Manager" "salary" => 7500 ] 2 => array:3 [▼ "name" => "Jacob Hill" "job" => "Web Developer" "salary" => 6500 ] ] }
Now we will filter all records from our original collection $collection
to list all employees with the job title as a web developer.
$filteredCollection = $collection->where('job', 'Web Developer'); dd($filteredCollection);
You will be presented with something like the below output.
Collection {#668 ▼ #items: array:3 [▼ 2 => array:3 [▼ "name" => "Jacob Hill" "job" => "Web Developer" "salary" => 6500 ] 3 => array:3 [▼ "name" => "Charles Brown" "job" => "Web Developer" "salary" => 5000 ] 4 => array:3 [▼ "name" => "Richard Wilson" "job" => "Web Developer" "salary" => 5400 ] ] }
You might have noticed that in the previous examples we provided the three arguments $key
, $operator
and $value
. But later on we just supplied the $key
and $value
.
If you don’t provide any $operator
Laravel will set the operator to =
.
The where()
method support the following operators.
Operator | Description |
---|---|
== | Ensures equality between the check $value and the key value on the collection item. This operator does not compare the types of the values. |
= | Same behavior as == . |
=== | Ensures equality between the check $value and the key value on the collection item. This operator does compare the types of the values. |
<> | Ensures inequality between the check $value and they key value on the collection item. This operator does not compare the types of the values. |
!== | Ensures inequality between the check $value and the key value on the collection item. This operator does compare the types of the values. |
< | Ensures that the key value on the collection item is less than the supplied check $value . |
> | Ensures 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 . |
Final Words
Laravel collection’s where()
method is very powerful to filter down a large collection of data. Laravel also provides the where method in the Eloquent which filters down the records from the database, this method works exactly the same as the Eloquent one.