这个问题看起来很傻,但这让我感到烦恼。
----------------------------------------------------------------
| account_id | order_id | sales | profit | currency | date |
|---------------------------------------------------|-----------
| 10 | 100 | 550 | 10 | USD |2016-10-11|
| 10 | 101 | 144 | 4 | NZD |2016-10-12|
| 9 | 102 | 429 | 44 | NZD |2016-10-13|
| 10 | 103 | 797 | 80 | NZD |2016-10-14|
----------------------------------------------------------------
我想按货币计算利润总和
$account = App\Account::find(10);
$collection = $account
->orders() # relation between order and account
->completedLastMonth() # scope of dates,
->groupBy('currency') # group by currency
->sum('profit') # sum of profit
但是$collection
是94。
所以我想要像这样的结果
$collection = [
'USD' => 10, # total profit of USD 10 for account 10
'NZD' => 84 # total profit of NZD 80 + 4 = 84 for account 10
]
我有帐户和订单与上个月范围之间的关系逻辑,请帮我查看汇总和GROUP BY
答案 0 :(得分:1)
$account = App\Account::find(10);
$collection = $account
->orders() # relation between order and account
->completedLastMonth() # scope of dates,
->groupBy('currency') # group by currency
->selectRaw('sum(profit) as sum, currency')
->lists('sum','currency');