Группировать можно с помощью Collections
:
$items = [
[
"categoria" => 1,
"ore" => "3.00"
],
[
"categoria" => 2,
"ore" => "2.00"
],
[
"categoria" => 3,
"ore" => "6.00"
],
[
"categoria" => 1,
"ore" => "6.00"
],
[
"categoria" => 2,
"ore" => "4.00"
]
];
$result = collect($items)
->groupBy("categoria")
->map(function ($item) {
$item = collect($item);
return [
"categoria" => $item[0]["categoria"],
"ore_sum" => $item->sum("ore")
];
});
dd($result->toArray());
Вот результат:
array:3 [▼
1 => array:2 [▼
"categoria" => 1
"ore_sum" => 9.0
]
2 => array:2 [▼
"categoria" => 2
"ore_sum" => 6.0
]
3 => array:2 [▼
"categoria" => 3
"ore_sum" => 6.0
]
]