Laravel:如何计算每月注册的新学生人数和在线学生人数?

时间:2019-10-04 03:35:36

标签: php laravel

我想计算每月注册的新学生人数和在线学生人数。我想要这样的输出:

必需的输出

"month" "total"  
--------------
"Jan"   "0"
"Feb"   "0"
"Mar"   "0"
"Apr"   "0"
"May"   "0"
"Jun"   "22"
"Jul"   "0"
"Aug"   "30"
"Sep"   "0"
"Oct"   "0"
"Nov"   "0"
"Dec"   "0"

但是返回的结果是: 输出

"month" "total"  
--------------  
"Jun"   "22"
"Aug"   "30"

这是我的代码


$getTotalOnl = Member::select(
    DB::raw('IFNULL(count(id),0) as count'),
    DB::raw('YEAR(updated_at) year, MONTH(updated_at) month')
     )->whereBetween('updated_at', [$from, $to])->groupby('year','month')->get()->toArray();
     dd($getTotal);

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

public function register() {
  $month = 12
  totalStudents = [];

  for($i = $month; $i > 0; $i--) {
    $students = Student::whereMonth('created_at', $i)->get();
    totalStudents[] = $i . '=' . $students->count(); 
  }

  dd($totalStudents);
}

$i代表日期,如果$i = 01表示为january,则可以说明该逻辑