所以我能够按照一些教程来构建莫里斯图表,这就是我到目前为止所想出的
$range = \Carbon\Carbon::now()->subDays(30);
$stats = DB::table('tickets')
->where('created_at', '>=', $range)
->groupBy('date')
->orderBy('date', 'ASC')
->get([
DB::raw('Date(created_at) as date'),
DB::raw('COUNT(*) as value')
])
->toJSON();
输出
"[{"date":"2018-11-10","value":1},{"date":"2018-11-11","value":1}]"
问题是我试图按月获得结果,而不是每天获取机票状态。
js代码
Morris.Area({
element: 'morris-area-chart'
, data: data
, xkey: 'date'
, ykeys: ['status']
});
机票表
id|phone_model |cus_name |issue |notes |created_at |updated_at |status
预期产量
[{"date":"11","tickets":2,"status":1},{"date":"11","tickets":1,"status":2},
{"date":"10","tickets":3,"status":1},{"date":"10","tickets":1,"status":3},]
我尝试了多种方法来获得基于月份和门票状态的结果,但是没有运气 任何帮助将不胜感激,谢谢。
答案 0 :(得分:1)
您可以使用以下查询:
$result_arr = \DB::select("SELECT DATE_FORMAT(created_at, '%m') date, status, COUNT(*) count FROM tickets GROUP BY date, status ORDER BY date DESC");
return collect($result_arr)->toJson();
例如输出这样的结果(输出在mysql控制台中):
+------+--------+-------+
| date | status | count |
+------+--------+-------+
| 12 | 1 | 1 |
| 12 | 2 | 3 |
| 12 | 4 | 1 |
| 12 | 10 | 7 |
| 11 | 1 | 1 |
| 11 | 2 | 1 |
| 11 | 4 | 1 |
| 10 | 1 | 3 |
| 10 | 2 | 2 |
| 10 | 3 | 1 |
| 10 | 4 | 2 |
| 09 | 1 | 2 |
| 09 | 2 | 7 |
| 09 | 3 | 5 |
| 08 | 1 | 1 |
| 08 | 2 | 1 |
| 08 | 12 | 1 |
| 07 | 1 | 4 |
| 06 | 1 | 1 |
| 06 | 2 | 12 |
| 06 | 3 | 2 |
| 05 | 1 | 1 |
| 05 | 2 | 8 |
| 05 | 11 | 1 |
| 04 | 1 | 2 |
| 04 | 2 | 8 |
| 04 | 10 | 1 |
| 03 | 1 | 2 |
| 02 | 1 | 2 |
| 02 | 2 | 26 |
| 02 | 3 | 1 |
| 02 | 11 | 2 |
| 02 | 12 | 1 |
| 01 | 2 | 21 |
| 01 | 3 | 8 |
| 01 | 10 | 6 |
+------+--------+-------+
36 rows in set (0.00 sec)
请记住,当您要基于两个status & date
对结果进行分组时,每个日期(可能有多个状态)不会只给您一行,因此每个date
对于每个status
,您可能会有不同的值。
有关DATE_FORMAT
的更多信息,请参见Mysql Docs。