0

I am trying to return all users from my Laravel 5.5 database and group them by the month they registered like this..

$users = DB::table('users')
     ->orderBy('created_at')
     ->groupBy(DB::raw("MONTH(created_at)"))
     ->get();

But this is giving me an error

Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column

Where am I going wrong?

** Update **

The exact error mesage I am getting is...

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'myapp.users.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `name`, `email`, MONTH(created_at) month from `users` group by `month` order by `created_at` asc)
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • https://stackoverflow.com/questions/23921117/disable-only-full-group-by –  Feb 19 '18 at 21:20

1 Answers1

0

You can try as:

$users = DB::table('users')
->select('name','email',DB::raw('MONTH(created_at) month'))
->orderBy('created_at', 'asc')
->groupby('month')
->get();
Luke
  • 943
  • 6
  • 12
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51