Laravel显示最近30天的数据

时间:2018-04-01 06:38:06

标签: php laravel

我有一个功能,我从数据库中获取Facebook用户注册数。

    $today = $now->today()->toDateString();
    $last_month = $now->subDays(30)->toDateString();

    $facebook_users = \App\Models\SocialLogins::join(
        'users',
        'users.id',
        '=',
        'social_logins.created_by'
    )
        ->where(['users.active' => 1,'source' => 1, 'user_type' => 3])
        ->whereBetween('users.created_utc',[$last_month,$today])
        ->get(['users.id','users.created_utc']);

所以,我在今天和02/03/2018之间得到了ID,这是正确的。现在,我必须按日期显示此数据,例如03/03/2018 x每天注册的人数。

我对此感到困惑,比如怎么做,有人知道怎么做吗?提前致谢

1 个答案:

答案 0 :(得分:0)

使用groupBy(date)

$facebook_users = \App\Models\SocialLogins::join(
    'users',
    'users.id',
    '=',
    'social_logins.created_by'
)
->where(['users.active' => 1,'source' => 1, 'user_type' => 3])
->whereBetween('users.created_utc',[$last_month,$today])
->groupBy(DB::raw('date(created_utc)'))
->selectRaw('date(created_utc) as date, count(1) as cnt')
->orderBy('date', 'asc')
->get();

foreach ($facebook_users as $facebook_user) {
    $date = $facebook_user->date;
    $cnt = $facebook_user->cnt;
}