skip to Main Content

In Laravel using Route::resource, the method should be "DELETE" instead of "GET" to delete a row/record so I have to use Form like this.

<form action="{{ route('category.destroy', $category->id]) }}" method="POST">
    @csrf
    @method('DELETE')
    <button type="submit" class="btn btn-danger btn-block">Delete</button>
</form>

it is working fine but I want to confirm before deleting.
I can show bootstrap model for confirmation but am unable to pass and use the $category->id in the model.

is there any better way.

2

Answers


  1. Chosen as BEST ANSWER

    Finally, I've done it like this. I used <a> tag to show delete confirmation model and passed route and category->id to href.

    <a href="{{ route('category.destroy', $category->id) }}" 
    class="text-danger w-4 h-4 mr-1 btnDelete"
    data-toggle="modal"
    data-target="#deleteCategoryModel">
    <i class="fa-solid fa-trash" style="color: #d11010;"></i>
    </a>
    

    included the from in the bootstrap model with action = ""

    then in jQuery I get the value of href of clicked <a> tage and passed it to action attribute of the form in the bootstrap model.

    $('#deleteCategoryModel').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget) // Button that triggered the modal
                var route = $(button).attr('href');
                $('#delete-category-form').attr('action', route);
            })
    

    and this is the complete solution.


  2. You can user bootstrap modal but i suggest you sweet alert its easy and perfect . You will see a confirmation message before you delete the record and if you want delete record using bootstrap modal use this above form inside modal like image i have sent.enter image description here

    Good Luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search