Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel collection search case insensitive

Tags:

php

laravel

I am searching through a collection and I would like to have this search case insensitive or at least change the collections values in lowercase. How can I do that?

$value = strtolower($value);
$collection = $collection->where($attribute, $value);

$value is lowercase while the content in the collection not, so there are no matches.

like image 558
Stefano Maglione Avatar asked Jul 05 '17 09:07

Stefano Maglione


1 Answers

You could instead use the filter() method with a callback that does what you want:

$collection = $collection->filter(function ($item) use ($attribute, $value) {
    return strtolower($item[$attribute]) == strtolower($value);
});
like image 54
Martin Bean Avatar answered Oct 23 '22 18:10

Martin Bean