简体   繁体   中英

Laravel display last 30 days data

I have got a function in which I am getting the Facebook Users registered count from database.

    $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. Now, I have to display this data datewise, like on 03/03/2018 x amount of people registered just the count for each day.

I am confused about this like how to do it, do anyone know how to do it? Thanks in advance

Use 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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