Collection Where LIKE Laravel 5.4
On a collection you can use the filter($callback_function)
method to select items in the collection. Pass in a callback function that returns true
for every item that should be returned.
In your case you can use the stristr()
function to emulate a LIKE
operator, like this:
collect($products)->filter(function ($item) use ($productName) {
// replace stristr with your choice of matching function
return false !== stristr($item->name, $productName);
});
Just replace stristr
with preg_match
or what ever you need to match strings.
This will return the collection in its original structure minus the non matching items.