Laravel 产品总价
我需要我的网店订单中的价格总和我有:Laravel 产品总价,laravel,eloquent,Laravel,Eloquent,我需要我的网店订单中的价格总和我有: @foreach($order->orderrow as $orderrow) {{ $product = $orderrow->product->latest_price->price }} @endforeach 这将显示订单中的所有价格, 订单是用订单生成的。这里是订单的产品,价格在一个表中 房间里有 产品型号 public function price() { return $thi
@foreach($order->orderrow as $orderrow)
{{ $product = $orderrow->product->latest_price->price }}
@endforeach
这将显示订单中的所有价格,
订单是用订单生成的。这里是订单的产品,价格在一个表中
房间里有
产品型号
public function price() {
return $this->hasMany(Price::class);
}
public function latest_price() {
return $this->hasOne(Price::class)->orderBy('date', 'desc');
}
当我尝试用最新的价格计算价格时,我的数据库中的所有价格都会增加
@foreach($order->orderrow as $orderrow)
{{ $product = $orderrow->product->latest_price->sum('price') }}
@endforeach
它将汇总价格表中的所有价格。您可以
汇总控制器中$order
的总价,然后将其与$order
一起发送到视图:
$totalOrderPrice = 0;
foreach($order->orderrow as $orderrow){
$totalOrderPrice += $orderrow->product->latest_price->price;
}
return view('yourViewName')->with(['totalOrderPrice'=>$totalOrderPrice,'order'=>$order]);
您可以在控制器中对$order
的总价进行sum
计算,然后将其与$order
一起发送到视图:
$totalOrderPrice = 0;
foreach($order->orderrow as $orderrow){
$totalOrderPrice += $orderrow->product->latest_price->price;
}
return view('yourViewName')->with(['totalOrderPrice'=>$totalOrderPrice,'order'=>$order]);
一种选择是对集合使用该方法:
$totalOrderPrice = $order->orderrow->sum(function ($orderRow) {
return $orderRow->product->latest_price->price;
});
以上假设$order->orderrow
是一个集合。如果不是,您可以简单地用collect(…)
将其包装,以获得相同的结果,即:
$totalOrderPrice = collect($order->orderrow)->sum(function ($orderRow) {
return $orderRow->product->latest_price->price;
});
一种选择是对集合使用该方法:
$totalOrderPrice = $order->orderrow->sum(function ($orderRow) {
return $orderRow->product->latest_price->price;
});
以上假设$order->orderrow
是一个集合。如果不是,您可以简单地用collect(…)
将其包装,以获得相同的结果,即:
$totalOrderPrice = collect($order->orderrow)->sum(function ($orderRow) {
return $orderRow->product->latest_price->price;
});
Makdous即使订单显示在users show.blade中,这也是可能的。php@RicardoPlompen是的。即使订单显示在users show.blade中,这也是可能的。php@RicardoPlompen是的。你想得到整个订单的总和吗?是的,我想得到整个订单的总和。你想得到整个订单的总和吗订单?是的,我想计算整个订单的金额