---
tags: Laravel, mysql, database, sql mode, group by
---
# 非完整的欄位設定 Group by 無法作用的解決方案
## 方案一
在 config/database.php 設定檔中設定MySQL的模式,而不是直接關掉 `strict` ,這是比較正規安全的做法
```php=
// config/database.php
'connections' => [
//...
'mysql' => [
//...
'strict' => true,
'modes' => [
//'ONLY_FULL_GROUP_BY', // Disable this to allow grouping by one column
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
//'NO_AUTO_CREATE_USER', // This has been deprecated and will throw an error in mysql v8
'NO_ENGINE_SUBSTITUTION',
],
],
],
```
## 方案二
在 config/database.php 設定檔中直接關掉 `strict`
```php=
// config/database.php
'connections' => [
//...
'mysql' => [
//...
'strict' => false,
//...
],
],
```