title: How to update cart item quantitty with ajax in laravel?
body: <p>I want to increment/decrement cart quantity by clicking the button. See this image <a href="https://i.stack.imgur.com/MRm8G.png" rel="nofollow noreferrer">preview image</a></p>
  <p>This cart row is shown by forcach loop. First row is working perfectly. The problem is, when I click on the seceond/last row, I get only first row value. I don't know how to solve that. Here is view code</p>
  <pre><code>    @foreach ($carts as $cart)
                              &lt;input type=&quot;hidden&quot; name=&quot;cart_id&quot; class=&quot;id&quot; value=&quot;{{ $cart-&gt;id }}&quot;&gt;
                              &lt;tr class=&quot;woocommerce-cart-form__cart-item cart_item&quot;&gt;
                                  &lt;td class=&quot;product-remove&quot;&gt;
                                      &lt;a href=&quot;#&quot; class=&quot;remove&quot; onclick=&quot;removeCart({{ $cart-&gt;id }})&quot;
                                          aria-label=&quot;Remove this item&quot;&gt;
                                          &lt;span class=&quot;lnr lnr-cross-circle&quot;&gt;&lt;/span&gt;
                                      &lt;/a&gt;
                                  &lt;/td&gt;
  
                                  &lt;td class=&quot;product-thumbnail&quot;&gt;
                                      &lt;a href=&quot;&quot;&gt;
                                          &lt;img src=&quot;{{ asset('/storage/items/food/' . $cart-&gt;FoodItem-&gt;image) }}&quot;
                                              class=&quot;attachment-shop_thumbnail size-shop_thumbnail wp-post-image&quot; alt=&quot;&quot; /
                                              height=&quot;60&quot; width=&quot;80&quot;&gt;
                                      &lt;/a&gt;
                                  &lt;/td&gt;
  
                                  &lt;td class=&quot;product-name&quot; data-title=&quot;Product&quot;&gt;
                                      &lt;a href=&quot;shop-single.html&quot;&gt;{{ $cart-&gt;FoodItem-&gt;name }}&lt;/a&gt;
                                  &lt;/td&gt;
  
                                  &lt;td class=&quot;product-price&quot; data-title=&quot;Price&quot;&gt;
                                      &lt;span class=&quot;woocommerce-Price-amount amount&quot;&gt;&lt;span
                                              class=&quot;woocommerce-Price-currencySymbol&quot;&gt;$&lt;/span&gt;{{ $cart-&gt;price }}&lt;/span&gt;
                                  &lt;/td&gt;
                                  &lt;td class=&quot;product-quantity&quot; data-title=&quot;Quantity&quot;&gt;
                                      &lt;div class=&quot;quantity&quot;&gt;
                                          &lt;input type=&quot;number&quot; class=&quot;input-text qty text input-quantity &quot;
                                              class=&quot;quantity&quot; step=&quot;1&quot; min=&quot;0&quot; name=&quot;quantity[]&quot;
                                              value=&quot;{{ $cart-&gt;quantity }}&quot; title=&quot;Qty&quot; size=&quot;4&quot;&gt;
                                          &lt;div class=&quot;icon&quot;&gt;
                                              &lt;a href=&quot;#&quot; class=&quot;number-button  plus&quot;&gt;+&lt;/a&gt;
                                              &lt;a href=&quot;#&quot; class=&quot;number-button minus&quot;&gt;-&lt;/a&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/td&gt;
                                  &lt;td class=&quot;product-subtotal&quot; data-title=&quot;Total&quot;&gt;
                                      &lt;span class=&quot;woocommerce-Price-amount amount&quot;&gt;&lt;span
                                              class=&quot;woocommerce-Price-currencySymbol&quot;&gt;$&lt;/span&gt; &lt;span
                                              class=&quot;sum&quot;&gt;{{ $cart-&gt;total_price }}&lt;/span&gt;&lt;/span&gt;
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                          @endforeach
  </code></pre>
  <p>Here is ajax code</p>
  <pre><code>    $(document).on('click', '.qty', function(e) {
          e.preventDefault();
  
          var quantity = $('.input-quantity').val();  
          var id = $('.id').val();
  
            $.ajax({
              url: &quot;url&quot;,
              method: &quot;POST&quot;,
              dataType: &quot;json&quot;,
              data: {
                  _token: &quot;{{ csrf_token() }}&quot;,
  
                  quantity: quantity,
                  id: id,
              },
  </code></pre>
  <p>Here my controller code</p>
  <pre><code>   $cart = Cart::findOrFail($id);
      $cart-&gt;update([
          'quantity'    =&gt; $request-&gt;quantity,
          'total_price' =&gt; $cart['price'] * $request-&gt;quantity,
  
      ]);
  </code></pre>
  <p>Can anyone tell me way how to solve that problem?</p>