【问题标题】:How to update product quantity after 'approved' status?“已批准”状态后如何更新产品数量?
【发布时间】: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


    【解决方案1】:

    您想在订单获得批准后更新产品表数量。对??您可以通过以下方式进行操作

    public function status_orders(Order $order ,Request $request) {
        if($request->status == 'approved') {
            foreach($order->products as $product) {
                $product->update([
                    'quantity' => $product->quantity - $product->pivot->quantity
                ]); 
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2019-12-19
      • 1970-01-01
      • 2022-11-26
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      相关资源
      最近更新 更多