我想使用laravel中的条形图创建计数每月数据,
$monthly_assessment = DB::table('praapplication')
->select(DB::raw('count(id) as total'), DB::raw('MONTH(created_at) as month'))
->where('stage', 'W1')
->groupBy('month')
->get();
我得到的输出如下:
total:2, month:10 ; total:3, month:11
代码输出应该像这样用Javascript表示
data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0]
如何用JavaScript解析
name: 'Submission',
data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0]
答案 0 :(得分:0)
以这种方式返回您的数据。
$monthly_assessment = DB::table('praapplication')
->select(DB::raw('count(id) as total'), DB::raw('MONTH(created_at) as month'))
->where('stage', 'W1')
->groupBy('month')
->get();
return response()->json($monthly_assessment , 200, array(), JSON_PRETTY_PRINT);
答案 1 :(得分:0)
我通常做的是..
$list = [];
// loop all 12 month
foreach(range(1, 12) as $month) {
$flag = false; // init flag if no month found in montly assessment
foreach($monthly_assessment as $data) {
if ($data->month == $month) { // if found add to the list
$list [] = $data->count;
$flag = true;
break; // break the loop once it found match result
}
}
if(!$flag) {
$list [] = 0; // if not found, store as 0
}
}
return $data = [
'name' : 'Submission',
'data' : $list
];
输出:
{
name: 'Submission',
data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0]
}