Laravel 12 E-Commerce Project - Admin Delete Coupon
Laravel 12 E-Commerce Project - Admin Delete Coupon
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 takes the coupon ID, finds the record, deletes it, and redirects back to the coupon list with a success 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"); -
Add the Delete Form/Link to the Coupons View
Open
coupons.blade.php. Inside the `` for the action column, integrate a form with the DELETE method. This is crucial for security and adherence to RESTful conventions. <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 ensure a record is not accidentally deleted, add a JavaScript confirmation dialog using the SweetAlert library. Use the `@push` and `@endpush` directives to include the script at the end of the page (assuming the layout loads the necessary libraries).
@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> @endpushDisplay Success Message
Finally, display the success status message at the top of the coupon list view,
coupons.blade.php, using the Laravel Session helper.<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 a coupon. A confirmation dialog will appear. Click Yes, and the coupon will be deleted, displaying the success message at the top of the page.
Thanks for watching this video. Don'tforget to subscribe to my channel!