在laravel eloquent 中如何解决与sql_mode=only_full_group_by 不兼容的问题? [英] How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?
问题描述
我的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_id
order 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
wherestatus
= 1 andstock
> 0 andcategory_id
= 5 group bystore_id
order byupdated_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屋!