I have been already to this link but its different in my case.
I have record which has a column created_at
. My objective is to group the record by year, and inside each year, records should be group in month also.
My expectations:
I have tried this code below, but it is working only for yearly only.
$records = \App\VehicleRental::query()
->where('operator_id', $operator->id)
->get()
->groupBy(function($val) { return \Carbon\Carbon::parse($val->created_at)->format('Y'); });
So I have tried to put another group for month but error Property [created_at] does not exist on this collection instance. returned
$record = \App\VehicleRental::query()
->where('operator_id', $operator->id)
->get()
->groupBy(function($val) { return \Carbon\Carbon::parse($val->created_at)->format('M'); })
->groupBy(function($val) { return \Carbon\Carbon::parse($val->created_at)->format('Y'); });
Someone knows how to achieve this?