In this video we are going to learn about Update Cart Quantity
So let see how can we update the quantity in cart.
For updating the cart quantity lets create a function inside the CartController


public function updateCart(Request $request)
{
Cart::instance('cart')->update($request->rowId,$request->quantity);
return redirect()->route('cart.index');
}


Now create route for this function
So go to the web.php file and create a new route here


Route::put('/cart/update', [CartController::class, 'updateCart'])->name('cart.update’);


Now go to the cart.blade.php file and here add a form as following


<form id="updateCartQty" action="{{route('cart.update')}}" method="POST">
@csrf
@method('put')
<input type="hidden" id="rowId" name="rowId" />
<input type="hidden" id="quantity" name="quantity" />
</form>


Now add here push directive and inside the script tag and now create a javascript function as following


@push('scripts')
<script>
function updateQuantity(qty)
{
$('#rowId').val($(qty).data('rowid'));
$('#quantity').val($(qty).val());
$('#updateCartQty').submit();
}
</script>
@endpush


And inside the input number filed add the data-rowid="{{$item->rowId}}"
Now lets call this function using onclick event from this input number filed as following


<td>
<div class="qty-box">
<div class="input-group">
<input type="number" name="quantity" data-rowid="{{$item->rowId}}" onchange="updateQuantity(this)" class="form-control input-number" value="{{$item->qty}}">
</div>
</div>
</td>


Now its done so lets check this
So first of all lets add product to the cart
Now click on up arrow for increasing product quantity by one
You can see here no of product has been increased by one
And also increased the subtotal and total price
Now lets decrease the product quantity
So, for that just click on down arrow
Here you can see no of product has been reduced
If I click one more time on this down icon
Then item has been removed from the cart
So in this way you can update the cart quantity in laravel 10 ecommerce project.