skip to Main Content

I want to update stock value when order will place. The challenge is that the products have different attributes place in the attribute table. Product Attribute Table

Small, Medium, Large, Extra large 

I want to update the product attribute stock respectively when order is place. For example when user place order user selected products like

product_id = 1 size Small quantity is 7

So 7 quantity must be decrement from the product attribute size Small column.

Checkout

// 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();

       
    }

}

When i run this code it shows me an error

InvalidArgumentException
Non-numeric value passed to decrement method.

When i enter value directly like decrement(‘stock’, 7) it shows and error

IlluminateDatabaseQueryException
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)

i search alot but yet not find any solution. Please any one help

2

Answers


  1. you have to warp it in an array or use two of wheres better and understandable

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

    Or like this:

    $item->where('product_id', $item['product_id'])
         ->where('size', $item['size'])
         ->decrement('stock', (int) $request->quantity);
    
    Login or Signup to reply.
  2. Just change the update query and make it simple and clean. The rest of the code is fine.

    // 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]);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search