Laravel 12 E-Commerce Project - Removing Coupon from Cart
Laravel 12 E-Commerce Project - Removing Coupon from Cart
Laravel 12 E-Commerce Project Tutorial
Admin: Deleting a Coupon
Hi everyone, welcome back to the Laravel E-Commerce Project Tutorial. This video focuses on implementing the functionality to delete a coupon from the admin panel. Let'sreview the steps to implement the deletion feature.
-
Create the Delete Function in AdminController
Open your
AdminControllerand create thecoupon_deletefunction. This function accepts the coupon ID, finds the record, deletes it, and redirects back to the coupon list with a success status message.public function coupon_delete($id) { $coupon = Coupon::find($id); $coupon->delete(); return redirect()->route("admin.coupons")->with("status", "Coupon has been deleted successfully!"); } -
Define the Route for Deletion
In
web.php, define a DELETE route that maps to the new function and includes the coupon ID:Route::delete("/admin/coupon/{id}/delete",[AdminController::class,"coupon_delete"])->name("admin.coupon.delete"); -
Integrate the Delete Form/Link into the Coupons View
Open
coupons.blade.php. Inside the table body'saction column (<td>), integrate a form that uses the DELETE method. This is essential for security and RESTful compliance.<td> <div class="list-icon-function bis_skin_checked="1"> <a href="{{ route("admin.coupon.edit", ["id"=>$coupon->id]) }}"> <div class="item edit bis_skin_checked="1"> <i class="fa fa-edit"></i> </div> </a> <form action="{{ route("admin.coupon.delete", ["id"=>$coupon->id]) }}" method="POST"> @csrf @method("DELETE") <button type="submit" class="item text-danger delete bis_skin_checked="1" style="background:none; border:none; padding:0;"> <i class="fa fa-trash"></i> </button> </form> </div> </td> -
Implement Confirmation Dialog using SweetAlert
To prevent accidental deletions, add a JavaScript confirmation dialog using the SweetAlert library. Use the
@push("scripts")directive at the end ofcoupons.blade.php(or in a section that will be included in the main layout).@push("scripts") <script> $(function() { $(".delete").on("click", function(e) { e.preventDefault(); var form = $(this).closest("form"); swal({ title: "Are you sure?", text: "You want to delete this record?", type: "warning", buttons: ["No", "Yes"], confirmButtonColor: "#dc3545" }).then(function(result) { if (result) { form.submit(); } }); }); }); </script> @endpush -
Display the Success Message
Finally, open
coupons.blade.phpand add a conditional block to display the success status message at the top of the table if it exists in the session:<div class="white_card_body"> <div class="table-responsive m-b-30"> @if(Session::has("status")) <p class="alert alert-success">{{Session::get("status")}}</p> @endif <table class="table table-striped table-bordered"> <!-- Table content (thead, tbody, etc.) follows --> ...
Verification 🗑️
Refresh the Admin Coupons Page. Click the Delete icon for any coupon. A confirmation dialog will appear. Click Yes to confirm the deletion. The coupon will be removed, and a success message will be displayed at the top of the page.
Thanks for watching this video. Don'tforget to subscribe to my channel!