1

enter image description hereThe Problem occur when I add group by in my query...

Form::select('*')->groupBy('serial_num')->orderBy('id')->Paginate(10);

SQLSTATE[42000]: Syntax error or access violation: 1055 'database.form.id' isn't in GROUP BY (SQL: select * from form group by serial_num limit 10 offset 0)

Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38

3 Answers3

2

This is not related to Laravel. You‘re selecting non-aggregated columns (*) in combination with grouping.

You have two options here

  1. use aggregation (Min, Max, Avg, Group_concat) on each column you‘re selecting and not grouping by
  2. disable ONLY_FULL_GROUP_BY on the MySQL server

More detailed explanation https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

Edit: Apparently Laravel can set MySQL modes on the fly as well.

As mentioned in the comments, you can disable strict mode in the Laravel database settings, Laravel then should set the MySQL server mode accordingly, I don’t find up-to-date documentation for this tho

https://github.com/laravel/laravel/blob/master/config/database.php#L59

https://mattstauffer.com/blog/strict-mode-and-other-mysql-customizations-in-laravel-5-2/

make sure you clear any config cache after changing the setting

Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77
1

Go to config\database.php

Set

'mysql' => [
    'strict' => false

after you saved, run this command

php artisan optimize
Shankar S Bavan
  • 922
  • 1
  • 12
  • 32
1

If you could handle the Pagination by yourself, then you could obtain the results and then implement groupBy on the collection.

$forms = Form::orderBy('id')->get()->groupBy(function ($item, $key) {
    return $item['serial_num'];
});
linktoahref
  • 7,812
  • 3
  • 29
  • 51