【发布时间】:2020-02-14 17:56:13
【问题描述】:
我有这些表:
产品:
id - name - quantity - etc
2 watch 4
4 T-shirt 2
订单:
id - user_id - address_id - etc
它们之间的数据透视表称为order_product
order_id - product_id - quantity
1 2 3
1 4 1
关系:
/**
* Products table relation
*/
public function products()
{
return $this->belongsToMany('App\Models\Product')->withPivot('quantity');
}
/**
* Orders table relation
*/
public function orders()
{
return $this->belongsToMany('App\Models\Order')->withPivot('quantity');
}
当我将状态更新为已批准然后更新我的数量产品时,我需要!
我的关闭:(在这里我很困惑,因为产品可以很多!数量也是如此)
public function status_orders(Order $order ,Request $request)
{
if($request->status == 'approved'){
$product_quantity = $order->products->pluck('quantity');
foreach($order->products as $order){
$order_quantity = $order->pivot->quantity;
}
$final_quantity = $product_quantity - $order_quantity;
}
}
【问题讨论】:
标签: laravel