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() public function cart()
{ {
$cartItems = DB::table('products') $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.*') ->select('products.name', 'products.price', 'products.picture', 'products.quantity as pQuantity', 'carts.*')
->where('carts.customerId', session()->get('id')) ->where('carts.customerId', session()->get('id'))
->get(); ->get();
@ -140,11 +141,6 @@ class MainController extends Controller
return view('cart', compact('cartItems')); return view('cart', compact('cartItems'));
} }
public function addToCart(Request $data) public function addToCart(Request $data)
{ {
if (session()->has('id')) { 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() public function checkout()
{ {
return view('checkout'); return view('checkout');

View File

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

View File

@ -11,6 +11,7 @@ Route::get('/blogDetails', [MainController::class, 'blogDetails']);
Route::get('/cart', [MainController::class, 'cart']); Route::get('/cart', [MainController::class, 'cart']);
Route::post('/addToCart', [MainController::class, 'addToCart']); Route::post('/addToCart', [MainController::class, 'addToCart']);
Route::get('/deleteCartItem/{id}', [MainController::class, 'deleteCartItem']);
Route::get('/checkout', [MainController::class, 'checkout']); Route::get('/checkout', [MainController::class, 'checkout']);
Route::get('/main', [MainController::class, 'main']); Route::get('/main', [MainController::class, 'main']);