Using where() to filter items from Collection

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 provided $value.

You can also pass a parameter to control how method compares $key with $value. Laravel runs the given command and returns a new collection of instances containing filtered items that satisfied 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)
{
    ...
}

Some Examples

The following code displays 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 less than operator as a second argument to filtering people having age less than 16.

$filtered = $collection->where('age', '<', 16);

dd($filtered);

Result will be something like:

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 with where() are:

OperatorsDescription
= 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 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.
<Makes 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.

Total
0
Shares
Leave a Reply

Your email address will not be published.

Related Posts
Total
0
Share