$productCategory = ProductCategory::query() ->select('category_id', 'product_id', 'category_name') ->whereIn('product_id', $spuIdArr) ->where('status', ProductCategory::STATUS_VALID) ->get() ->groupBy('product_id') ->toArray();
// groupBy需要放在get()的后面,不然分組之后只有一條數據。
sortBy(),針對collection中的某一個字段進行排序(默認是升序排序)
$collection = collect([
['name' => 'Desk', 'price' => 200],
['name' => 'Chair', 'price' => 100],
['name' => 'Bookcase', 'price' => 150],
]);
$sorted = $collection->sortBy('price');
$sorted->values()->all();
/*
[
['name' => 'Chair', 'price' => 100],
['name' => 'Bookcase', 'price' => 150],
['name' => 'Desk', 'price' => 200],
]
*/