Laravel 12 E-Commerce Project

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.

  1. Create the Delete Function in AdminController

    Open your AdminController and create the coupon_delete function. 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!");
    }
    
  2. 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");
    
  3. 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>
    
  4. 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 of coupons.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
    
  5. Display the Success Message

    Finally, open coupons.blade.php and 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!

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.