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.

#laravel 7/6 #javascript #php #laravel

Laravel 7/6 Collections Filter Method Example
9.60 GEEK