1

I have cart on my web store and so far everything works perfectly but I said when I try decrement the product quantity to product, the all product in my database will decrement Not just products in cart.

Here is my ordercontroller :

public function postOrder(Request $request) {    
    $validator = Validator::make($request->all(), [
        'first_name' => 'required|max:30|min:2',
        'last_name'  => 'required|max:30|min:2',
        'address'    => 'required|max:50|min:4',
        'address_2'  => 'max:50|min:4',
        'city'       => 'required|max:50|min:3',
        'state'      => 'required|',
        'zip'        => 'required|max:11|min:4',
        'full_name'  => 'required|max:30|min:2',
    ]);

    if ($validator->fails()) {
        return redirect('/checkout')
            ->withErrors($validator)
            ->withInput();
    }

    $first_name = Input::get('first_name');
    $last_name = Input::get('last_name');
    $address = Input::get('address');
    $address_2 = Input::get('address_2');
    $city = Input::get('city');
    $state = Input::get('state');
    $zip = Input::get('zip');
    $full_name = Input::get('full_name');        
    $user_id = Auth::user()->id;
    $cart_products = Cart::with('products')->where('user_id', '=', $user_id)->get();
    $cart_total = Cart::with('products')->where('user_id', '=', $user_id)->sum('total')        
    $charge_amount = number_format($cart_total, 2) * 100;        
    $order = Order::create (
        array(
            'user_id'    => $user_id,
            'first_name' => $first_name,
            'last_name'  => $last_name,
            'address'    => $address,
            'address_2'  => $address_2,
            'city'       => $city,
            'state'      => $state,
            'zip'        => $zip,
            'total'      => $cart_total,
            'full_name'  => $full_name,
        ));

    foreach ($cart_products as $order_products) {
        $order->orderItems()->attach($order_products->product_id, array(
            'qty'    => $order_products->qty,
            'price'  => $order_products->products->price,
            'reduced_price'  => $order_products->products->reduced_price,
            'total'  => $order_products->products->price * $order_products->qty,
            'total_reduced'  => $order_products->products->reduced_price * $order_products->qty,
        ));
    }

// in the fact all product will decrement Not just products in cart
    \DB::table('products')->decrement('product_qty', $order_products->qty);

    Cart::where('user_id', '=', $user_id)->delete();

    flash()->success('Success', 'Your order was processed successfully.');

    return redirect()->route('cart');

}

I use program \DB::table('products')->decrement('product_qty', $order_products->qty);; for the decrement but in the fact all product will decrement Not just products in cart

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

1 Answers1

0

The decrement you are using updates all records, as you have observed because you didn't constrain it to a specific record. You want to do this in your loop:

DB::table('products')
            ->where('id', '=', $order_products->product_id)
            ->decrement('product_qty', $order_products->qty);
Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
  • Thank you Scott for your answer, when I try this code, I get error `strtolower() expects parameter 1 to be string, array given` then I change the code with : `\DB::table('products') ->where('id', $order_products->product_id) ->decrement('product_qty', $order_products->qty);` no error but only one product in the cart will decrement. example, I buy two or three product, when I checkout only one product in product database table will decrement while the other product, qty does not change. – ubaidillahumar Jul 31 '17 at 15:25
  • Thanks you Mr, the problem was solved after I include this code to looping `foreach ($cart_products as $order_products) { $order->orderItems()->attach($order_products->product_id, array( 'qty' => $order_products->qty, ........................................... )); \DB::table('products') ->where('id', '=', $order_products->product_id) ->decrement('product_qty', $order_products->qty); }` – ubaidillahumar Aug 02 '17 at 01:22