[英]Laravel Query Builder use parent query in subquery of `whereIn` statement?
Laravel 8.x Laravel 8.x
I'm trying to do something like mentioned over here我正在尝试做类似这里提到的事情
(code copied from above link- minor change to comment) (从上面的链接复制的代码-对评论的微小更改)
$total = OrderTotals::whereIn('order_id', function($q) use($query) {
// here would like to use the query of the parent instead of an empty fresh query
})->sum('value);
Where I would like to use the previous query in the subquery when calling ->whereIn
is this possible how can I do this?调用时我想在哪里使用子查询中的上一个查询
->whereIn
这可能吗?我该怎么做?
The $query
variable in the closure is empty and produces a select *
without any from or wheres.闭包中的
$query
变量为空,并产生一个select *
没有任何 from 或 wheres。 This make sense since a subquery can be wanted to be fresh.这是有道理的,因为可以希望子查询是新鲜的。 But what if I would like to use the existing query?
但是如果我想使用现有的查询呢?
I used this google search among many others to find a similar question.我在许多其他人中使用了这个谷歌搜索来找到类似的问题。 The results on stackoverflow don't seem to use the existing query, they all add new
from
s and where
s (see here and this highly rated question ). stackoverflow 上的结果似乎没有使用现有的查询,它们都添加了新
from
s 和where
s(参见这里和这个高度评价的问题)。
See if something like this helps看看这样的事情是否有帮助
Model::where(function($query)
{
$query->whereIn('order_id', [1,2,3]);
//OR
$query->or_where('order_id', function(){
// do something
});
})->where('other condition')->sum('value');
this may also work:这也可能有效:
OrderTotals::addSelect(['order_id' => Order::select('id')
->whereColumn('active', 0)
->orderByDesc('completed_at')
])->sum();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.