Search code examples
phplaravelormlaravel-5laravel-5.3

Group by not working after upgrading to laravel 5.3


This is the code that was working on laravel 5.2

$menus = CmsMenuItem::groupBy('menu_id')->get();

but now it throws error

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'convertifier_cms.cms_menu_items.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from 'cms_menu_items' group by 'menu_id')

I have also tried

 `strict => false` 

in database.php but no effect


Solution

  • Try this for database config.

     'mysql' => [
                'driver' => 'mysql',
                'host' => env('DB_HOST', 'localhost'),
                'port' => env('DB_PORT', '3306'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'charset' => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix' => '',
                'strict' => false,
                'engine' => null,
            ],
    

    and use query like this way

    $menus =DB::table('cms_menu_item')
        ->select('*')
        ->groupBy('menu_id')
        ->get();