简体   繁体   中英

Delete handled by resource controller doesn't work - Laravel 5.3

I'm using resource controller in Laravel 5.3 and I'm having problem with deleting a record. I would like to use simple HTML code and I know that I have to add a hidden method input to make it work.

My code is very simple:

<form action="{{ url('/task', $task->id) }}">
    {{ method_field('DELETE') }}
    <input type="submit" value="Delete" />
</form>

After clicking submit app redirects to blank page - it doesn't go to destroy function in controller. I don't have any idea, why it's not working. I'm not using facades, is it necessary in operation like this? I'll be very glad for every tip, thank you.

You're most likely running into a TokenMismatchException . Laravel considers the DELETE method a "writable" method, so it expects a CSRF token.

You can either add a CSRF token to your form, or, if appropriate, you can add your URI to the except array in your app/Http/Middleware/VerifyCsrfToken.php file.

To add the token to your form:

<form action="{{ url('/task', $task->id) }}">
    {{ method_field('DELETE') }}
    {{ csrf_field() }}
    <input type="submit" value="Delete" />
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM