Laravel : 下单后更新库存价值

Laravel : Update Stock Value After Order Placement

我想在下订单时更新库存价值。 挑战 是产品在属性 table 中的属性位置不同。

Small, Medium, Large, Extra large 

我想在下订单时分别更新产品属性库存。例如,当用户下订单时,用户选择的产品如

product_id = 1 size Small quantity is 7

因此 7 的数量必须从产品属性尺寸 递减

结帐

// checkout
public function checkout(Request $request)
{
    if ($request->isMethod('post')) {
        $data = $request->all();
        
        DB::beginTransaction(); 

       
        // Get cart details (Items)
        $cartItems = Cart::where('user_id', Auth::user()->id)->get()->toArray();
        foreach ($cartItems as $key => $item) {
            # code...
            $cartItem = new OrdersProduct;
            $cartItem->order_id = $order_id;
            $cartItem->user_id = Auth::user()->id;

            // Get products details
            $getProductDetails = Product::select('product_code', 'product_name', 'product_color')->where('id', $item['product_id'])->first()->toArray();

            $cartItem->product_id = $item['product_id'];
            $cartItem->product_code = $getProductDetails['product_code'];
            $cartItem->product_name = $getProductDetails['product_name'];
            $cartItem->product_color = $getProductDetails['product_color'];
            $cartItem->product_size = $item['size'];
            $getDiscountedAttrPrice = Product::getDiscountedAttrPrice($item['product_id'], $item['size']);

            $cartItem->product_price = $getDiscountedAttrPrice['final_price'];
            $cartItem->product_qty = $item['quantity'];

            $cartItem->save();

            // Want to Update the Product Attribute Table Stock
            $item = new ProductsAttribute;
            $item->where('product_id', '=', $item['product_id'], 'size', '=', $item['size'])->decrement('stock', $request->quantity);
        }

        // Insert Order Id in Session variable for Thanks page
        Session::put('order_id', $order_id);

        DB::commit();

       
    }

}

当我 运行 这段代码显示错误时

InvalidArgumentException
Non-numeric value passed to decrement method.

当我直接输入值如递减('stock', 7) 它显示错误

Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '`product_id` is null' at line 1 (SQL: update `products_attributes` set `stock` = `stock` - 7, `products_attributes`.`updated_at` = 2021-05-01 17:18:30 where size `product_id` is null)

我搜索了很多但还没有找到任何解决方案。请任何人帮助

你必须将它扭曲成一个数组,或者使用其中两个更好更易理解的地方

$item->where([
    ['product_id', '=', $item['product_id']],
    ['size', '=', $item['size']],
])->decrement('stock', (int) $request->quantity);

或者像这样:

$item->where('product_id', $item['product_id'])
     ->where('size', $item['size'])
     ->decrement('stock', (int) $request->quantity);

只需更改更新查询并使其简单明了。其余代码没问题。

// Want to Update the Product Attribute Table Stock
$product_attribute = ProductsAttribute::where(['product_id' => $item['product_id'], 'size' => $item['size']])->first();
if($product_attribute){
    $stock = $product_attribute->stock - (int) $request->quantity;
    $product_attribute->update(['stock' => $stock]);
}