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?
The $query
variable in the closure is empty and produces a select *
without any from or 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 ).
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.