AppDividend
Latest Code Tutorials

Laravel Collections Filter Method Tutorial With Example

0

Collection filter() is an inbuilt Laravel method that calls PHP array_filter() method on the underlying array, which, according to the PHP docs, preserves the array keys.  Laravel Eloquent uses Collections to return the results. Collections contain useful methods that make them very powerful and helpful to use. You can filter them, modify them and much more with them very conveniently. This then results in your array being converted to a JavaScript object instead of an array.

Laravel 6 Collections Filter Method

The filter() method filters the collection using the given callback, keeping only those items that pass a given truth test. If you are not familiar with Laravel Collections, then check out my Laravel Collections Example guide. 

You can find the collection filter method inside Illuminate\Support\Collection class. See the method below.

public function filter(callable $callback = null)
{
     if ($callback) {
         return new static(Arr::where($this->items, $callback));
     }

     return new static(array_filter($this->items));
}

The filter function takes a callback as an argument and run filter over each item. If the test fails for a particular item, then it will remove it from the collection.

Now, let’s see the example in action. For that, you need to install Laravel in your machine. I have already done it.

From now on, we will test each Laravel Collections Methods inside the routes >> web.php file.

Write the following code inside the web.php file.

<?php

// web.php

Route::get('/', function () {
    $collection = collect([19, 21, 29, 46]);

    $filtered = $collection->filter(function ($value, $key) {
        return $value > 21;
    });

    dd($filtered->all());
});

So, we have checked each collection item against 21 and if any item > 21 in the collection then it will be included in a new array.

Now, start the Laravel server by typing the following command in your project root.

php artisan serve

Go to the http://localhost:8000, and you will see the following output.

Laravel Collections Filter Method Tutorial With Example

That means it has created an array with all the items > 21. Rest items will be removed from the collection.

If no callback is supplied, all entries of the collection that are equivalent to false will be removed.

<?php

// web.php

Route::get('/', function () {
    $collection = collect([0, 1, 2, 3, 4, 5]);

    $filtered = $collection->filter();

    dd($filtered->all());
});

In the above code, entry will be removed because it has taken as a boolean 0 which is false. The output is the following.

Laravel Collections Filter Method

Finally, Laravel Collections Filter Method Example is over.

See also

PHP array_filter() example

Laravel collections search()

Laravel eloquent example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.