I have the following method in laravel that basically gets all the parameters that are passed by url, and inside the loop it checks that they are valid and if so it filters the collection data with the method where()
that comes with Laravel collections .
protected function filterData(Collection $collection, $filters)
{
foreach (request()->query() as $query => $value) { // Bucle por los parámetros
if (isset($query, $value) && in_array($query, $filters)) { // Validar
$collection = $collection->where($query, $value); // Filtrar con where()
// $collection = $collection->like($query, $value); // Método deseado
}
}
return $collection;
}
The problem I'm having is that I'd like the filter to act like a "LIKE" instead of a "WHERE" . That is, if the filter is ?name=Carlos
, let me also select the value "Carlos Appelidos"
.
I have found a solution in SO English . Basically I have replaced this part:
For this other:
Where the mb_strpos function is used (which looks for the position of the first occurrence of a string in another string) and if it does not find the string, the return returns true and causes the method
reject()
to remove it from the results.