Laravel 5.5 Collection where like

后端 未结 1 327
野性不改
野性不改 2021-01-27 21:39

i am filtering data using collections. But i need to use like method. I had tried to write like this : (\'name\', \'LIKE\', \'%value%\') but it did not work.

相关标签:
1条回答
  • 2021-01-27 22:13

    The 1st question is whether you really know what you are doing. If you take data from database and then filter it just to take some elements it's definitely not the best way because you can take from database for example 100000 records just to finally have only 2 elements and it will kill your application performance.

    But assuming you really want to filter using Support collection, there is no where together with LIKE because you are just filtering an array. If you want to use something similar to like instead of:

    $collection = $collection->where('name', $value);
    

    you can use:

    $collection = $collection->reject(function($element) use ($value) {
        return mb_strpos($element->name, $value) === false;
    });
    

    depending on what you really have in collection instead of $element->name you might need to use $element['name']

    0 讨论(0)
提交回复
热议问题