Resolving MySQL ONLY_FULL_GROUP_BY Error in Laravel by Adjusting sql_mode and Strict Settings
This article explains how to troubleshoot and fix the MySQL 5.7 ONLY_FULL_GROUP_BY error in a Laravel application by inspecting sql_mode, understanding Laravel's strict mode behavior, and customizing the framework's mode configuration to remove the problematic setting without changing global MySQL settings.
After upgrading to MySQL 5.7, the default ONLY_FULL_GROUP_BY mode caused a syntax error when selecting non‑aggregated columns without grouping.
The author first checked the current sql_mode using SELECT @@GLOBAL.sql_mode; and SELECT @@SESSION.sql_mode; , discovering that the session mode included ONLY_FULL_GROUP_BY .
Attempts to modify the MySQL configuration file ( my.cnf ) and client settings did not affect the error, leading to the realization that Laravel sets the sql_mode per connection.
By printing the sql_mode from Laravel code, it was confirmed that the framework applied strict mode, which adds ONLY_FULL_GROUP_BY .
Inspecting Laravel’s vendor/laravel/framework/src/Illuminate/Database/Connectors/MySqlConnector.php revealed the setModes method that either uses a custom modes array or the strict flag to set the session sql_mode.
Setting 'strict' => false removed ONLY_FULL_GROUP_BY , but the author preferred to keep strict mode and explicitly define the modes without ONLY_FULL_GROUP_BY :
'strict' => true,
'modes' => ['STRICT_TRANS_TABLES','NO_ZERO_IN_DATE','NO_ZERO_DATE','ERROR_FOR_DIVISION_BY_ZERO','NO_AUTO_CREATE_USER','NO_ENGINE_SUBSTITUTION']This configuration resolves the error without altering MySQL’s global settings.
In summary, the issue was caused by Laravel’s strict mode adding ONLY_FULL_GROUP_BY ; the fix is to customize the modes array or disable strict mode.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.