Deja123
Deja123

Reputation: 109

DELETE request on a link in laravel 5.4

I am trying to hit a route I made to make a delete HTTP request in laravel view when user clicks on 'Delete' button, but it won't work. I've read it should be done with forms in laravel.

Here is my code:

<form action="/admin/pages/delete/{{ $section->id }}" method="post">
    {{ method_field('delete') }}
    <button class="btn btn-sm" type="submit">Delete</button>
</form>

What is a proper way to handle this? It shows me an error in the console, Bootbox: 'please specify a message' whenever I click on the button.

Route definition inside admin group:

    Route::delete('/pages/delete/{id}', 'PagesController@delete')->name('pages.delete');

Upvotes: 0

Views: 337

Answers (2)

Yves Kipondo
Yves Kipondo

Reputation: 5603

You must add the the CSRF Field because all form submission must past through the VerifyCsrfToken middleware before the request be procede by the controller

{{ csrf_field() }} // add this before or after the {{ method_field() }}

Upvotes: 1

Ybhaw
Ybhaw

Reputation: 145

I believe you are missing the csrf token in the form. You can add

{{ csrf_field() }}

just after your form starts.

Visit this link for knowing more about csrf

Upvotes: 1

Related Questions