Q:

Collection Where LIKE Laravel 5.4

I know collection doesn't support where LIKE but how can I achieve this.
My data is:

collect($products)->where('name', 'LIKE', '%'. $productName . '%');

Laravel doesn't support where like in the collection. I'm thinking to use 
collect($products)->filter( function () use ($productName) {

            return preg_match(pattern, subject);
        })

but I don't know how to do it. TY

A:

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.

