laravel 5.3 以后默认开启 mysql严格模式(strict
)
在mysql在严格模式下, 并且开启了ONLY_FULL_GROUP_BY的情况下,
group by 的字段没有出现在 select 的语句中会报错.关闭了严格模式就不会报错.
如下Demo
public static function orders($phase)
{
return self::select(DB::raw("SUM(orders) as orders"),"type","users_id","name")
->groupBy("type","name")
->where("phase",$phase)
->get();
}
会出现如下错误,当然*号内的内容是不同的
Syntax error or access violation: 1055 '***' isn't in GROUP BY
解决办法
laravel
出现上述错误是sql_mode
设置导致的问题,修改config/database.php
配置文件的strict => false
即可关闭了严格模式:
'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,
],
原创:https://phpartisan.cn/news/77.html