在laravel eloquent 中如何解决与sql_mode=only_full_group_by 不兼容的问题? [英] How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?

查看:62
本文介绍了在laravel eloquent 中如何解决与sql_mode=only_full_group_by 不兼容的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的laravel口才是这样的:

My laravel eloquent is like this :

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

执行时,出现如下错误:

When executed, there exist error like this :

SQLSTATE[42000]:语法错误或访问冲突:1055 表达式 #1SELECT 列表不在 GROUP BY 子句中并且包含非聚合myshop.products.id"列在功能上不依赖GROUP BY 子句中的列;这与sql_mode=only_full_group_by (SQL: select * from products wherestatus = 1 and stock > 0 and category_id = 5 group by store_idorder by updated_at desc limit 4)

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'myshop.products.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from products where status = 1 and stock > 0 and category_id = 5 group by store_id order by updated_at desc limit 4)

我该如何解决?

推荐答案

我遇到了类似的问题,通过在数据库连接设置中禁用 mysql 严格模式来解决.

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,
    ]
]

您可以在 Matt Stauffer 的这篇博文

这篇关于在laravel eloquent 中如何解决与sql_mode=only_full_group_by 不兼容的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆