有一個orders表的total列包含超過 1000 條記錄。我如何獲得每天的每日總和total。我的意思是周一、周四、周三……有很多不同的行,我想分別顯示total每一天的總和?在 UI 中為:
saturday: 111211,
sunday: 211444,
Monday: 120012000,
Thursday: 1225121,

uj5u.com熱心網友回復:
您可能正在尋找功能DAYOFWEEK。
DB::table('orders')
->selectRaw('DAYOFWEEK(created_at) as weekday, SUM(total) as sales')
->groupBy('weekday')
->get();
(代碼未經測驗)
uj5u.com熱心網友回復:
MySQL 查詢: SELECT DAYNAME(created_at) as day, SUM(total) as total FROM orders group by day
Laravel 查詢生成器:
DB::table('orders')
->selectRaw('DAYNAME(created_at) as day, SUM(total) as total')
->groupBy('day')
->get();
uj5u.com熱心網友回復:
我使用 ORM,這對我有用:
$sales = Order::all()->groupBy(function ($date) {
return $date->created_at->format('Y-m-d');
})->map(function ($item) {
return [
"date" => item[0]->created_at,
"total" => $item->sum('total'),
];
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/425345.html
