Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lilo's avatar
Level 2

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

0 likes
2 replies
a4ashraf's avatar
a4ashraf
Best Answer
Level 33

@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.