升级到 laravel 5.3 后不工作分组

Group by not working after upgrading to laravel 5.3

这是用于 laravel 5.2

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

但现在它抛出错误

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')

我也试过了

 `strict => false` 

在 database.php 但没有效果

根据此 PR,只需在您的数据库配置中尝试此操作

'strict' => false,

如果不是,则存在一些已知问题。

请参考这些链接PR & Issue

试试这个数据库配置。

 '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,
        ],

并像这样使用查询

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

请转到您的 config/database.php 文件夹。在 mysql 配置数组中,将 strict => true 更改为 strict => false,一切都会很好地工作。