我需要在Laravel中为图表创建查询。
我需要创建这个json文件:
例如:(30天前)
[{
"day": "23",
"count": 20
}, {
"day": "22",
"visits": 10
}, {
"day": "21",
"visits": 12
}, {
"day": "20",
"visits": 30
}, {
"day": "19",
"visits": 20
}, {
"day": "18",
"visits": 10
},
.
.
.
.
}, {
"day": "22",
"visits": 10
}]
我在Laravel的代码是:
$date = Carbon::today()->subDays(30);
$ordersChart = Order::selectRaw('dayname(created_at) day, count(*) count')
->where('created_at', '>=', $date)
->groupBy('day')
->latest()
->get();
但在返回$ordersChart
后显示此错误:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'laravel.orders.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select dayname(created_at) day, count(*) count from `orders` where `created_at` >= 2018-03-20 00:00:00 group by `day` order by `created_at` desc)
如何发出此问题?
答案 0 :(得分:1)
您必须使用->latest('day')
。
答案 1 :(得分:0)
你必须使用:
1- dayname(created_at)
=> day(created_at)
2- latest()
=> latest('day')
$ordersChart = Order::selectRaw('day(created_at) day, count(*) count')
->where('created_at', '>=', $date)
->groupBy('day')
->latest('day')
->get();