$request
から取得した日付に基づいて、各日付のビュー数を送信する必要があります。私は行方不明の日付と0
のビューの数を追加する必要があり、そのデータについてはLaravel - データベースから失われた日付と数を入力してください
[{"time":"2016-05-01","count":2},{"time":"2016-05-02","count":3},{"time":"2016-05-03","count":7},{"time":"2016-05-07","count":3}]
:コントローラの私のクエリから、私はこのようになりますチャートのデータを取得します。私はthis exampleに従うことを試みているが、私の例ではこの解決策を実装できないようだ。これは私の方法である:
public function timelines(Request $request){
$from = $request['from'];
$to = $request['to'];
$data = DB::table($request['option'])
->select(DB::raw('DATE(created_at) as time'), DB::raw('count(*) as count'))
->whereDate('created_at', '>=', date($from).' 00:00:00')
->whereDate('created_at', '<=', date($to).' 00:00:00')
->groupBy('time')
->get();
return json_encode($data);
}
更新コード:これにより
public function timelines(Request $request){
$from = $request['from'];
$to = $request['to'];
$date = $from;
$data = [];
if ($request['option'] == 'time'){
$data = View::groupBy('time')
->orderBy('time', 'ASC')
->whereDate('created_at', '>=', date($from).' 00:00:00')
->whereDate('created_at', '<=', date($to).' 00:00:00')
->get([
DB::raw('Hour(created_at) as time'),
DB::raw('COUNT(*) as "count"')
]);
}
else {
while($date <= $to) {
$formattedDate = date('Y-m-d', strtotime($date));
$results = DB::table($request['option'])
->select(DB::raw('DATE(created_at) as time'), DB::raw('count(*) as count'))
->whereDate('created_at', '=', $formattedDate .' 00:00:00')
->groupBy('time')
->get();
if(count($results)) {
$data[] = [
'time' => $formattedDate,
'count' => $results[0]->count
];
} else {
$data[] = [
'time' => $formattedDate,
'count' => 0
];
}
$date = strtotime('+1 day', strtotime($formattedDate));
}
}
return json_encode($data);
}
私は例えば、私が送る最初の日付のカウントを取得:
[{"time":"2016-03-16","count":0}]
'[{" time ":" 2016-03-16 "、" count ":0}]' $ request ['option'] ==のときに得られる結果です'time' 'または' $ request ['option']!= 'time''? – henrik
私は$ request ['option']!= 'time'を持っているときに起こりますが、私は既に解決策を思いつきました。 – Marco