$items = MyModel::whereYear('created_at', '2021')
->get()
->groupBy(function($events) {
return Carbon::parse($events->created_at)->format('m');
});
{
"11":[
{
"id":4,
"type":1,
"manager_id":6,
"order_id":2341,
"created_at":"2021-11-14T21:02:55.000000Z",
"updated_at":null
}
],
"12":[
{
"id":2,
"type":1,
"manager_id":1,
"order_id":2345,
"created_at":"2021-12-14T21:02:55.000000Z",
"updated_at":null
},
{
"id":3,
"type":1,
"manager_id":3,
"order_id":2344,
"created_at":"2021-12-10T21:02:55.000000Z",
"updated_at":null
}
]
}
$items = DB::table('table')
->whereYear('created_at', 2021)
->selectRaw('month(created_at) month, count(*) count')
->groupBy('month')
->get();