简体   繁体   English

Laravel显示最近30天的数据

[英]Laravel display last 30 days data

I have got a function in which I am getting the Facebook Users registered count from database. 我有一个功能,可以从数据库中获取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']);

So, I am getting the ids between today and 02/03/2018, which is correct. 所以,我得到今天和2018年2月3日之间的ID,这是正确的。 Now, I have to display this data datewise, like on 03/03/2018 x amount of people registered just the count for each day. 现在,我必须按日期显示此数据,例如在03/03/2018 x仅记录每天计数的人数。

I am confused about this like how to do it, do anyone know how to do it? 我对此很困惑,比如怎么做,有人知道怎么做吗? Thanks in advance 提前致谢

Use groupBy(date) : 使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM