评论删除laravel

时间:2020-05-10 17:42:41

标签: php laravel

我想删除评论,但是当我单击时,这会将我重定向到laravel错误页面并显示""该错误。为什么会发生?

web.php

 Route::post('/comments/destroy/{id}', 'CommentController@destroy')->name('comment.destroy');

评论控制器

 public function destroy($id){
    $comment=Comment::where('id',$id)->first();
    $comment->delete();
    return redirect()->back();
  }

刀片

@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

1 个答案:

答案 0 :(得分:2)

如果在html表单上添加{{ method_field('DELETE') }},则必须将route方法更改为delete

Route::delete('/comments/destroy/{id}', 'CommentController@destroy')->name('comment.destroy');

但是,如果您要在路线上使用POST方法,则必须从表单中删除{{ method_field('DELETE') }}