How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?

前端 未结 12 1596
不思量自难忘°
不思量自难忘° 2020-11-29 11:09

My laravel eloquent is like this :

$products = Product::where(\'status\', 1)
            ->where(\'stock\', \'>\', 0)
            ->where(\'category         


        
相关标签:
12条回答
  • 2020-11-29 11:44

    In folder config => database.php make sure mysql strict is false, like this

    'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8',
        'collation' => 'utf8_general_ci',
        'prefix' => '',
        'strict' => false,
        'engine' => null,
    ],
    

    if strict is true, make it false then clear config cash by run this command in cmd

    php artisan config:clear

    0 讨论(0)
  • 2020-11-29 11:44

    I solved it by setting modes in config/database.php file.

    Set modes as follows:

    'modes'  => [
                    'STRICT_TRANS_TABLES',
                    'NO_ZERO_IN_DATE',
                    'NO_ZERO_DATE',
                    'ERROR_FOR_DIVISION_BY_ZERO',
                    'NO_ENGINE_SUBSTITUTION',
                ]
    

    for mysql driver

    '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' => true,
            'engine' => null,
            'modes'  => [
                'ONLY_FULL_GROUP_BY',
                'STRICT_TRANS_TABLES',
                'NO_ZERO_IN_DATE',
                'NO_ZERO_DATE',
                'ERROR_FOR_DIVISION_BY_ZERO',
                'NO_ENGINE_SUBSTITUTION',
            ]
        ],
    
    0 讨论(0)
  • 2020-11-29 11:45

    I had a similar Problem and solved it by disabling mysql strict mode in the database connection setting.

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

    You can find even more configuration settings in this blog post by Matt Stauffer

    0 讨论(0)
  • 2020-11-29 11:46

    As said, set strict mode to false may give security bugs, what i am doing is to set sql_mode to empty before queries that require it. Note that it is a TEMPORARY change, once your connection is close (by laravel request) you will be set to original sql_mode=only_full_group_by (or beyond).

    DB::statement("SET sql_mode = '' ");

    Cheers, happy coding...

    ps.: its not laravel fault, if you try to execute this query directly on your DB you will face same result. This work around works in mysql as well as first statement and again, will be a temporary session change, not permanent.

    0 讨论(0)
  • 2020-11-29 11:48

    To select only aggregated columns, use the one of the raw methods provided by Laravel. For example:

    Product::selectRaw('store_id')
            ->where('status', 1)
            ->groupBy('store_id')
            ->get();
    
    0 讨论(0)
  • 2020-11-29 11:49

    Check the query:

    Product::where('status', 1)
                ->where('stock', '>', 0)
                ->where('category_id', '=', $category_id)
                ->groupBy('store_id')
                ->orderBy('updated_at', 'desc')
                ->take(4)
                ->get();
    

    here you are grouping the data by store_id and fetching all columns in the result set which is not allowed. To solve it either select store_id or aggregate function on it or change the system variable sql_mode=only_full_group_by to SET sql_mode = ''.

    Reference

    0 讨论(0)
提交回复
热议问题