darryldecode / laravelshoppingcart

Shopping Cart Implementation for Laravel Framework
1.33k stars 436 forks source link

Update RowID returns zero (0) quantity to the edited item #236

Open Jay-Dabo opened 4 years ago

Jay-Dabo commented 4 years ago

Anytime i add an item to cart and try to update the quantity, the page reloads and returns a quantity of zero to the item i edit.

Here's my CartController.php code:

public function update($rowId) {
        // Update Cart Quantity 
        \Cart::session(auth()->id())->update($rowId, [
            'quantity' => [
                'relative' => false,
                'value' => request('quantity')
            ]
        ]);

        return back();
    }

Here are my cart index.blade.phpcodes:

  1. The input field showing the Quantity of the product. Default is 1
    <!-- Quantity -->
    @foreach ($cartItems as $item)
    <div class="cart_item_quantity">
    <div class="container">
        <form class="product_quantity clearfix">
            <input name="quantity" type="number" value="{{ $item->quantity }}">
        </form>
    </div>
    </div>
    @endforeach
  2. The update button to the cart items. This button serves the entire cart and is not part of the FOR...EACH condition. Items with no changes to their quantity returns the same quantity (Default 1 or the number of ADD-TO-CART clicks done on the shop page as the quantity).
    <form action="{{route('cart.update', $item->id, [])}}">
    <input type="submit" class="button update_cart_button" name="" value="Update Cart">
    </form>

    I did a lot of switch backs between Laravel Collective and not plain HTML

Here's my route web.php code: Route::get('cart/update/{itemId}', 'CartController@update')->name('cart.update')->middleware('auth');

frezno commented 4 years ago

@Jay-Dabo how are you adding the item to the cart? using the users id as in the example:

// add cart items to a specific user $userId = auth()->user()->id; // or any string represents user identifier Cart::session($userId)->add(array( 'id' => 456, // inique row ID 'name' => 'Sample Item', 'price' => 67.99, 'quantity' => 4, 'attributes' => array(), 'associatedModel' => $Product ));

Jay-Dabo commented 4 years ago

@frezno

public function add(Product $product) {
        // Add to Cart
        \Cart::session(auth()->id())->add(array(
            'id' => $product->id,
            'name' => $product->product_name,
            'price' => $product->price,
            'quantity' => 1,
            'attributes' => array(),
            'associatedModel' => $product
        ));

        return back();
    }
Jay-Dabo commented 4 years ago

Any help here?