You need to assign the filtered values to something.
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Im trying to use filter() method for the collection in laravel 5.2. But the result returns the same result when I remove the filter() method. Please see my code below.
if(!empty(request('columns.1.search.value'))) {
$union->filter(function($item) {
return date_format(new \DateTime($item['SUBMITTED']), 'm/d/Y') == request('columns.1.search.value');
});
}
If you have a collection of arrays/collections you can do something like this
$filtered = $books->filter(function($value, $key) {
if($value['title'] == 'The Great Hunt') {
return $value;
}
});
That will give you all the books with the title of The Great Hunt.
I take it you are getting the data you want to filter from the database? Then you should let the database do the filtering since it's way faster and uses way less resources.
Please or to participate in this conversation.