Count results on specific date
Hi,
I want to take the data created in the last 7 days and make a chart from them.
I get the dates but could not count the number of data generated per day.
Here is my query:
$date = \Carbon\Carbon::today()->subDays(7);
$record = Report::where('created_at', '>=', $date)->get();
$numofrecord = $record->count();
How can I group these data day per day? Thanks a lot
@lilo
you can use groupby clause in your query
$record = Report::select([
DB::raw('DATE(created_at) AS date'),
DB::raw('COUNT(id) AS count'),
])
-> where('created_at', '>=', \Carbon\Carbon::today()->subDays(7) )
->groupBy('date')
->orderBy('date', 'ASC')
->get() ->toArray();
Please or to participate in this conversation.