我已經通過inventory_id 對列價格組求和。此外,我需要計算總價。$record = ProductDetails::with('inventoryName')->whereDate('created_at', '=', date($date)) ->select('inventory_id',DB::raw('sum(price) as item_price'),DB::raw('sum(quantity) as quantity'))->groupBy('inventory_id')->get();我的輸出是:[ { inventory_id: 9, item_price: 30, quantity: 30, inventory_name: [ { id: 9, name: "sugar" } ] }, { inventory_id: 10, item_price: 70, quantity: 70, inventory_name: [ { id: 10, name: "oil" } ] }]現在我需要計算總價。如何做到這一點。
1 回答

暮色呼如
TA貢獻1853條經驗 獲得超9個贊
您可以使用Laravel Collection: SUM 方法。
1. If $record is already a collection:
echo $record->sum('item_price');
2. If $record is an array.
$collection = collect($record);
$collection->sum('item_price');
- 1 回答
- 0 關注
- 294 瀏覽
添加回答
舉報
0/150
提交
取消