Home  >  Article  >  Database  >  How to Fix the \'Incompatible with sql_mode=only_full_group_by\' Error in Laravel Eloquent?

How to Fix the \'Incompatible with sql_mode=only_full_group_by\' Error in Laravel Eloquent?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-23 12:10:19292browse

How to Fix the

Resolving Incompatible Error with sql_mode=only_full_group_by in Laravel Eloquent

Laravel Eloquent queries can encounter the "Incompatible with sql_mode=only_full_group_by" error when the query includes non-aggregated columns in the SELECT list that are not functionally dependent on the columns in the GROUP BY clause. To resolve this issue, the MySQL strict mode can be disabled in the database connection settings.

In the example code provided, the query is grouping by store_id and ordering by updated_at, but the id column is also included in the SELECT list. Since id is not functionally dependent on store_id, it cannot be included in the SELECT list without aggregation.

To solve the error, disable MySQL strict mode by adding the following configuration to your database connection settings in the config/database.php file:

'connections' => [
    'mysql' => [
        // Behave like MySQL 5.6
        'strict' => false,

        // Behave like MySQL 5.7
        'strict' => true,
    ]
]

Disabling strict mode allows non-aggregated columns to be included in the SELECT list even if they are not functionally dependent on the columns in the GROUP BY clause.

Additional Configuration Settings

Further configuration options related to strict mode can be found in the blog post by Matt Stauffer:

  • https://mattsauer.com/blog/laravel-5-3-group-by-hack-with-mysql-strict-mode

The above is the detailed content of How to Fix the \'Incompatible with sql_mode=only_full_group_by\' Error in Laravel Eloquent?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn