Reputation: 383
I want delete a comment, but when i click, this redirect me to laravel error page and display ""
that error. Why is happen ?
web.php
Route::post('/comments/destroy/{id}', 'CommentController@destroy')->name('comment.destroy');
commentcontroller
public function destroy($id){
$comment=Comment::where('id',$id)->first();
$comment->delete();
return redirect()->back();
}
blade
@foreach($comment as $comments)
<form action="{{route('comment.destroy',$comments->id )}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn"><i class="fa fa-trash-o"></i></button>
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="comment-body"><p>{{ $comments->body }}</p></div>
</form>
@endforeach
Upvotes: 0
Views: 562
Reputation: 194
If you add {{ method_field('DELETE') }}
on the html form, you must change the route method to delete
:
Route::delete('/comments/destroy/{id}', 'CommentController@destroy')->name('comment.destroy');
But if you want to use POST
method on the route, you must remove {{ method_field('DELETE') }}
from your form.
Upvotes: 2