Skip to content
Advertisement

gor error message when deleteing comment in Laravel project

working with Laravel 6 project and I have following CommentController,

JavaScript

and My routes are as following

JavaScript

but when I try to delete comment got following validation error message The comment field is required.

how could I fix this problem here?

edit.blade.php

JavaScript

Advertisement

Answer

Because HTML forms can’t send PUT, PATCH and DELETE requests, we must “spoof” the request and include an input field that tells Laravel which type it is sending. You can read about that here.

Technically, this means inserting the following for your delete form.

JavaScript

Laravel comes with different helpers that help you achieve this. In your update form, you have already done this by adding the method_field('PUT');. Here, you are instructing Laravel that this is a PUT request, and we need to do this too in your delete form.

The {{ method_field('PUT') }} simply needs to change to {{ method_field('DELETE') }} or you can use the built-in @method('delete')

Like this

JavaScript

You can inspect your form HTML in the browser and notice the hidden input field added automatically.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement