Search code examples
jquerylaravel-bladeboot

How to confirm before deleting in Laravel Route:resource


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.


Solution

  • 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.