1$users = User::select('id', 'created_at')
2->get()
3->groupBy(function($date) {
4 //return Carbon::parse($date->created_at)->format('Y'); // grouping by years
5 return Carbon::parse($date->created_at)->format('m'); // grouping by months
6});
7
8$usermcount = [];
9$userArr = [];
10
11foreach ($users as $key => $value) {
12 $usermcount[(int)$key] = count($value);
13}
14
15for($i = 1; $i <= 12; $i++){
16 if(!empty($usermcount[$i])){
17 $userArr[$i] = $usermcount[$i];
18 }else{
19 $userArr[$i] = 0;
20 }
21}
22