升级到 Laravel 5.3 后不工作分组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39344897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Group by not working after upgrading to laravel 5.3
提问by msonowal
This is the code that was working on laravel 5.2
这是在 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')
SQLSTATE[42000]:语法错误或访问冲突:1055 SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且包含非聚合列“convertifier_cms.cms_menu_items.id”,它在功能上不依赖于 GROUP BY 子句中的列;这与 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
在 database.php 但没有效果
回答by Nil
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();
回答by vijaykumar
回答by M Arfan
Please Go to your config/database.php folder. In mysql configuration array, change strict => true to strict => false, and everything will work nicely.
请转到您的 config/database.php 文件夹。在 mysql 配置数组中,将严格 => true 更改为严格 => false,一切都会很好地工作。