如何在laravel php中获取最近30天的用户注册数

How to get last 30 days user register count also in those days not register count is 0 with sorting descending order add use Carbon\Carbon; in above. this way you can get a sorted data of last 30 days of user registered

$chartDatas = User::select([
      DB::raw('DATE(created_at) AS date'),
      DB::raw('COUNT(id) AS count'),
    ])
    ->whereBetween('created_at', [Carbon::now()->subDays(30), Carbon::now()])       
    ->groupBy('date')
    ->orderBy('date', 'DESC')
    ->get()
    ->toArray();

$chartDataByDay = array();
foreach($chartDatas as $data) {     
    $chartDataByDay[$data['date']] = $data['count'];
}
$date = new Carbon;
for($i = 0; $i < 30; $i++) {
  $dateString = $date->format('Y-m-d');
  if(!isset($chartDataByDay[ $dateString ])) {
    $chartDataByDay[ $dateString ] = 0;
  }
  $date->subDay();
}
$array = array();
foreach($chartDataByDay as $key => $value){
    $key = date('Y-m-d',strtotime($key));
    $array[$key] = $value;
}
ksort($array);
print_r($array);
duangutian1426
duangutian1426 尝试$userCountByDay=User::select([DB::raw('COUNT(id)AScount'),created_at])->whereBetween('created_at',[Carbon::now()->subDays(30),Carbon::now()])->orderBy('start_time')->get()->groupBy(function($val){returnCarbon::parse($val->created_at)->format('d');});和dd()结果
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐