[英]Laravel groupBy month and sum the total
[
{
"id": "1",
"date": "2019-06-22",
"total": "1200" // THIS TOTAL IS A MUTATOR
},
{
"id": "2",
"date": "2019-06-24",
"total": "2200" // THIS TOTAL IS A MUTATOR
},
{
"id": "3",
"date": "2019-07-20",
"total": "2300" // THIS TOTAL IS A MUTATOR
},
]
我想將這些數據按MONTH分組並返回總和
2019-06
總數為1200
AND 2019-07
總數為4500
$customer_branch = ExportInvoice::where('customer_branch_id', $customer_branch_id)
-> approved()
-> whereYear('date', $year)
-> orderBy('date', 'ASC')
-> get();
而且我嘗試循環拋出$customer_branch
並將數據設置為數組,但是沒有得到預期的結果
1.打開config/database.php
2.在mysql連接設置里面找到strict
密鑰
3.將值設置為false
4.然后運行php artisan config:cache
現在嘗試此查詢
ExportInvoice::select('id',\DB::raw('SUM(total) as total,DATE_FORMAT(date,"%Y-%m") as monthDate'))
->where('customer_branch_id', $customer_branch_id)
->approved()
->groupBy(\DB::raw('DATE_FORMAT(date,"%Y-%m")'))
->orderBy('date')
->get();
groupBy中的Simble DB:raw應該可以解決問題。
$customer_branch = ExportInvoice::where('customer_branch_id', $customer_branch_id)
->approved()
->whereYear('date', $year)
->groupBy(DB::raw(DATE_FORMAT(date,'%Y-%m'))
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.