Troubleshooting "Syntax error or access violation: 1055" in Laravel
When using WhereIn and GroupBy in the same query, you may encounter an error such as "SQLSTATE[42000]: Syntax error or access violation: 1055". This indicates a violation of SQL's requirement to have all non-aggregated columns included in the GROUP BY clause.
To resolve this issue, there are two options.
Disable Strict Mode
In your config/database.php file, under the 'mysql' array, set 'strict' to false. This will disable all strict mode settings.
Specify Allowed Strict Modes
Alternatively, you can leave 'strict' as true and add specific modes to the 'modes' option. To allow grouping by one column without including it in the GROUP BY clause, comment out the 'ONLY_FULL_GROUP_BY' mode.
'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', 'NO_ENGINE_SUBSTITUTION' ], ]
You can continue to use WhereIn and GroupBy without encountering the "1055" error.
Note: It's recommended to use strict mode for security and data integrity reasons. However, you may need to disable it temporarily for queries that require grouping by non-aggregated columns.
The above is the detailed content of How to Fix Laravel's 'Syntax error or access violation: 1055' Error When Using WhereIn and GroupBy?. For more information, please follow other related articles on the PHP Chinese website!