Search

Laravel 5 Collections: Filtering Collection Elements With where

April 22, 2018 —John Koster

The where method allows developers to filter a collection given a key value pair. It filters the collection's items by checking that the given $key has some value equal to the provided $value. An argument can be supplied for the $operator parameter to control how the method internally compares the given key against the value. The where method will return a new collection instance containing all the items the match the given criteria. The original collection instance will not be modified in any way.

#Signature

1public function where(
2 $key,
3 $operator,
4 $value = null
5);

#Example Use

The following code example shows the basic usage of the where method:

1<?php
2 
3use Illuminate\Support\Collection;
4 
5// Create a new collection.
6$collection = new Collection([
7 [
8 'genus' => 'Canis',
9 'name' => 'tundra wolf',
10 'age' => '2'
11 ],
12 [
13 'genus' => 'Canis',
14 'name' => 'arctic wolf',
15 'age' => 4
16 ],
17 [
18 'genus' => 'Cucumis',
19 'name' => 'cucumber',
20 'age' => 2
21 ]
22]);
23 
24// Get all animals with the genus 'Canis'
25$animals = $collection->where('genus', 'Canis');

After the above code has executed, the $animals variable will be an instance of Collection and contain a value similar to the following output:

1Illuminate\Support\Collection {
2 #items: array [
3 0 => array [
4 "genus" => "Canis"
5 "name" => "tundra wolf"
6 "age" => "2"
7 ]
8 1 => array [
9 "genus" => "Canis"
10 "name" => "arctic wolf"
11 "age" => 4
12 ]
13 ]
14}

An observant reader will notice that the where method was used with only two parameters used. When the where method is called with only two parameters, such as in where('genus', 'Canis'), the $operator parameter will internally be set to =. This allows the method to maintain backwards compatibility with older versions of the Collection class.

The where method supports 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.

Each item in the previous collection contains an age property. Some of these ages are stored as strings, and some are stored as integers. This will be sufficient to show the differences between the equality operators and how they are affected by type comparisons.

1<?php
2 
3// ...
4 
5$typeComparisonResults = $collection
6 ->where('age', '===', 2);

After the above code has executed, the $typeComparisonResults collection will only contain one item: the cucumber. This is because the cucumber's age is represented as an integer, and the value 2 was passed into the where method as an integer. If the value 2 was passed as a string the only value returned would have been the tundra wolf.

In Laravel versions 5.2 and below, similar functionality can be achieved using the filter method (the methods demonstrated below are still relevant for Laravel 5.3, however). The following example demonstrates how to use comparison operators with the filter method:

1<?php
2 
3// ...
4 
5// Get all items where the age is
6// greater than or equal to 4.
7$filteredResults = $collection->filter(function ($item) {
8 return data_get($item, 'age') >= 4;
9});

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.