Thursday, March 9, 2017

Laravel 5 - how to filter collection until specific count of items found?

Now I have such code:

return MyModel::all()->filter(function($value){
    return $value->attribute == 5;
})->slice(0, 6);

Value of attribute is loaded through accessor, so I can't just do where('attribute', 5) instead. Is there any standard way to optimize this, so filter will return only first 6 matched items?



via Denis Sheremet

Advertisement