vendredi 28 décembre 2018

Sort Collection By id set

I have an array of ids that i want to sort a given column by them in the collection.

For example,

$suppliersOrder = [8,7,5,1,3,4];

$items = Items::get()->sortBy(function($model) use ($suppliersOrder) {
   return array_search($model->supplier_id, $suppliersOrder);
})->values();

This acts like ordering items as [1,3,4,5,7,8] instead of the given order. And if I try sortByDesc, likewise [8,7,5,4,3,1] but I couldn't figure out the way to actually sort them as my given array's order.

My ultimate goal is then running $items->groupBy('supplier.name') so I can have my desired order.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire