Update Cart Items

This commit is contained in:
UronShrestha 2024-07-05 15:43:08 +05:45
parent 4a512fa736
commit 7b2a70ca1d
3 changed files with 72 additions and 31 deletions

View File

@ -163,6 +163,42 @@ class MainController extends Controller
return redirect()->back()->with('success', 'Item deleted from cart successfully!');
}
// public function updateCartItem(Request $data, $id)
// {
// if (session()->has('id')) {
// $item = Cart::find($data->input('id'));
// $item->quantity = $data->input('quantity');
// $item->save();
// return redirect()->back()->with('success', 'Item updated successfully!');
// } else {
// return redirect('/login')->with('error', 'Please login to update item!');
// }
// }
public function updateCartItem(Request $request, $id)
{
// dd($request->all());
if (session()->has('id')) {
$item = Cart::find($id);
if ($item) {
$item->quantity = $request->input('quantity');
$item->save();
return redirect()->back()->with('success', 'Item updated successfully!');
} else {
return redirect()->back()->with('error', 'Item not found!');
}
} else {
return redirect('/login')->with('error', 'Please login to update item!');
}
}
public function checkout()

View File

@ -43,38 +43,9 @@
</thead>
{{-- <tbody>
@foreach ($cartItems as $item)
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="{{ asset('uploads/products/' . $item->picture) }}" alt="">
</div>
</td>
<td class="quantity__item">
<div class="product__cart__item__text">
<h6>
{{ $item->name }}
</h6>
<h5>
<b>Nrs {{ $item->price }}</b>
</h5>
</div>
<div class="quantity">
<input type="number" class="form-control" name="quantity" min="1"
max="{{ $item->pQuantity }}" value="{{ $item->quantity }} ">
</div>
</td>
<td class="cart__price"> <br> Nrs. {{ $item->price * $item->quantity }} </td>
<td class="cart__close"><i class="fa fa-close"></i></td>
</tr>
@endforeach
</tbody> --}}
<tbody>
@foreach ($cartItems as $item)
<tr>
<td class="product__cart__item">
@ -93,13 +64,45 @@
</div>
</td>
<td class="cart__price"><br> Nrs. {{ $item->price * $item->quantity }}</td>
<td class="cart__close"> <a href="{{ url::to('deleteCartItem/' . $item->id) }}">
<i class="fa fa-trash"></i></a>
</td>
</tr>
@endforeach
</tbody> --}}
<tbody>
@foreach ($cartItems as $item)
<tr>
<td class="product__cart__item">
<div class="product__cart__item__pic">
<img src="{{ asset('uploads/products/' . $item->picture) }}" alt="">
</div>
</td>
<td class="quantity__item">
<div class="product__cart__item__text">
<h6>{{ $item->name }}</h6>
<h5><b>Nrs {{ $item->price }}</b></h5>
</div>
<div class="quantity">
<form action="{{ route('cart.update', $item->id) }}" method="post">
@csrf
<input type="number" class="form-control" name="quantity"
min="1" max="{{ $item->pQuantity }}"
value="{{ $item->quantity }}">
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</td>
<td class="cart__price"><br> Nrs. {{ $item->price * $item->quantity }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="row">

View File

@ -12,6 +12,8 @@ Route::get('/blogDetails', [MainController::class, 'blogDetails']);
Route::get('/cart', [MainController::class, 'cart']);
Route::post('/addToCart', [MainController::class, 'addToCart']);
Route::get('/deleteCartItem/{id}', [MainController::class, 'deleteCartItem']);
Route::post('updateCartItem/{id}', [MainController::class, 'updateCartItem'])->name("cart.update");
Route::get('/checkout', [MainController::class, 'checkout']);
Route::get('/main', [MainController::class, 'main']);