S
S
semki0962018-03-19 14:38:43
Laravel
semki096, 2018-03-19 14:38:43

Is there a way to group (groupBy) users by registration dates?

An example of grouping by user status in Laravel. And how to group users by registration date - for example, January February March, etc.?

$users = DB::table('users')
                     ->select(DB::raw('count(*) as user_count, status'))
                     ->where('status', '<>', 1)
                     ->groupBy('status')
                     ->get();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
metallix, 2018-03-19
@metallix

Try like this -

$users = DB::table('users')
                     ->select(DB::raw('count(*) as user_count, status'))
                     ->where('status', '<>', 1)
                     ->groupBy(function($user) {
                           return Carbon::parse($user->created_at)->format('m');
                       })->get();

A
Andrey, 2018-03-19
@VladimirAndreev

in select select DATE_FORMAT(t.reg_dt, '%Y%m') AS reg_ym
in groupBy - by reg_ym and group.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question