Delete Cart Items

This commit is contained in:
UronShrestha 2024-07-05 13:14:27 +05:45
parent 71944f3245
commit 4a512fa736
3 changed files with 24 additions and 9 deletions

View File

@ -130,7 +130,8 @@ class MainController extends Controller
public function cart()
{
$cartItems = DB::table('products')
->join('carts', 'carts.productId', '=', 'products.id')
// ->join('carts', 'carts.productId', '=', 'products.id')
->join('carts', 'carts.productId', 'products.id')
->select('products.name', 'products.price', 'products.picture', 'products.quantity as pQuantity', 'carts.*')
->where('carts.customerId', session()->get('id'))
->get();
@ -140,11 +141,6 @@ class MainController extends Controller
return view('cart', compact('cartItems'));
}
public function addToCart(Request $data)
{
if (session()->has('id')) {
@ -160,6 +156,15 @@ class MainController extends Controller
}
}
public function deleteCartItem($id)
{
$item = Cart::find($id);
$item->delete();
return redirect()->back()->with('success', 'Item deleted from cart successfully!');
}
public function checkout()
{
return view('checkout');

View File

@ -25,6 +25,13 @@
<div class="row">
<div class="col-lg-8">
<div class="shopping__cart__table">
@if (session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
@endif
<table>
<thead>
<tr>
@ -77,8 +84,8 @@
</td>
<td class="quantity__item">
<div class="product__cart__item__text">
<h6>{{ $item->name }}</h6>
<h5><b>Nrs {{ $item->price }}</b></h5>
<h6>{{ $item->name }} (<b>Nrs {{ $item->price }}</b>)</h6>
<h5></h5>
</div>
<div class="quantity">
<input type="number" class="form-control" name="quantity" min="1"
@ -86,7 +93,9 @@
</div>
</td>
<td class="cart__price"><br> Nrs. {{ $item->price * $item->quantity }}</td>
<td class="cart__close"><i class="fa fa-close"></i></td>
<td class="cart__close"> <a href="{{ url::to('deleteCartItem/' . $item->id) }}">
<i class="fa fa-trash"></i></a>
</td>
</tr>
@endforeach
</tbody>

View File

@ -11,6 +11,7 @@ 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::get('/checkout', [MainController::class, 'checkout']);
Route::get('/main', [MainController::class, 'main']);