Laravel querybuilder withCount 关系
Laravel querybuilder withCount relationship
我遇到脚本问题。
我正在使用 Laravel 6.x(但没有太大区别)。
所以,我有一个模型 Order
,它与另一个模型 Group
有这样的关系:
型号订单:
public function groups()
{
return $this->hasMany('App\Models\Order\Group', 'order_id')->orderBy('order_group');
}
模型组:
public function order()
{
return $this->belongsTo('App\Models\Order\Order', 'order_id');
}
我想要所有 Order
有一个组,其中组内的列 'is_recurrent'
是 true
并且至少有 2 行 'is_recurrent'
true
.
基本上是这样的:
$orders = Order::withCount(['groups' => function ($query) {
$query->where('is_recurrent', true);
}])
->having('groups_count', '>', 1)
->get();
我尝试过很多东西,比如 Order::whereHas('groups', 函数 ....).
在很多情况下我得到了:
SQLSTATE[42803]: Grouping error: 7 ERROR: column "ordergroups.order_group" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... "is_recurrent" = group by "order_id" order by "order_gro...
^ (SQL: select "orders"., (select count() from "ordergroups" where "orders"."id" = "ordergroups"."order_id" and "is_recurrent
" = 1 group by "order_id" order by "order_group" asc) as "groups_count" from "orders" having "groups_count" > 1 order by "created_at" desc)
有
$orders = Order::withCount(['groups' => function ($query) {
$query->where('is_recurrent', true)->groupBy('order_group');
}])
->having('groups_count', '>', 1)
->get();
我得到了:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "groups_count" does not exist
LINE 1: ...roup" asc) as "groups_count" from "orders" having "groups_co...
^ (SQL: select "orders"., (select count() from "ordergroups" where "orders"."id" = "ordergroups"."order_id" and "is_recurrent
" = 1 group by "order_group" order by "order_group" asc) as "groups_count" from "orders" having "groups_count" > 1 order by "created_at" desc)
如果有人有任何线索:D
withCount
函数向您的模型添加了一个变量,它在查询中不可用(至少据我所知)。
根据您 table 结构的少量信息,这应该会给出正确的结果;在相同的查询中使用 whereHas
,您将确保至少有 1 个组具有 is_recurrent = true
,而使用 withCount
将为您提供单个订单模型的完整计数。
$orders = Order::withCount(['groups' => function ($query) {
$query->where('is_recurrent', true)->groupBy('order_group');
}])
->whereHas(['groups' => function ($query) {
$query->where('is_recurrent', true)->groupBy('order_group');
}])
->get();
我遇到脚本问题。 我正在使用 Laravel 6.x(但没有太大区别)。
所以,我有一个模型 Order
,它与另一个模型 Group
有这样的关系:
型号订单:
public function groups()
{
return $this->hasMany('App\Models\Order\Group', 'order_id')->orderBy('order_group');
}
模型组:
public function order()
{
return $this->belongsTo('App\Models\Order\Order', 'order_id');
}
我想要所有 Order
有一个组,其中组内的列 'is_recurrent'
是 true
并且至少有 2 行 'is_recurrent'
true
.
基本上是这样的:
$orders = Order::withCount(['groups' => function ($query) {
$query->where('is_recurrent', true);
}])
->having('groups_count', '>', 1)
->get();
我尝试过很多东西,比如 Order::whereHas('groups', 函数 ....).
在很多情况下我得到了:
SQLSTATE[42803]: Grouping error: 7 ERROR: column "ordergroups.order_group" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ... "is_recurrent" = group by "order_id" order by "order_gro...
^ (SQL: select "orders"., (select count() from "ordergroups" where "orders"."id" = "ordergroups"."order_id" and "is_recurrent
" = 1 group by "order_id" order by "order_group" asc) as "groups_count" from "orders" having "groups_count" > 1 order by "created_at" desc)
有
$orders = Order::withCount(['groups' => function ($query) {
$query->where('is_recurrent', true)->groupBy('order_group');
}])
->having('groups_count', '>', 1)
->get();
我得到了:
SQLSTATE[42703]: Undefined column: 7 ERROR: column "groups_count" does not exist
LINE 1: ...roup" asc) as "groups_count" from "orders" having "groups_co...
^ (SQL: select "orders"., (select count() from "ordergroups" where "orders"."id" = "ordergroups"."order_id" and "is_recurrent
" = 1 group by "order_group" order by "order_group" asc) as "groups_count" from "orders" having "groups_count" > 1 order by "created_at" desc)
如果有人有任何线索:D
withCount
函数向您的模型添加了一个变量,它在查询中不可用(至少据我所知)。
根据您 table 结构的少量信息,这应该会给出正确的结果;在相同的查询中使用 whereHas
,您将确保至少有 1 个组具有 is_recurrent = true
,而使用 withCount
将为您提供单个订单模型的完整计数。
$orders = Order::withCount(['groups' => function ($query) {
$query->where('is_recurrent', true)->groupBy('order_group');
}])
->whereHas(['groups' => function ($query) {
$query->where('is_recurrent', true)->groupBy('order_group');
}])
->get();