亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Laravel 中使用了 groupby 的總和

在 Laravel 中使用了 groupby 的總和

PHP
呼喚遠方 2022-01-02 16:22:08
我已經通過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');


查看完整回答
反對 回復 2022-01-02
  • 1 回答
  • 0 關注
  • 294 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號