[Route:form.comments.destroy] [URI:form / {form} / comments / {comment}]缺少必需的参数(Laravel 5.6)

时间:2018-12-04 18:37:34

标签: php laravel laravel-5

我试图为表单上的注释实现一个非常简单的destroy方法,但我缺少了似乎无法动弹的东西。尝试运行视图页面时,出现错误:

ErrorException (E_ERROR)
Missing required parameters for [Route: form.comments.destroy] 
[URI: form/{form}/comments/{comment}]. 
(View: C:\distributor\resources\views\form\view.blade.php)

我的路线文件设置如下:

......
Route::resource('forms', 'FormController');
Route::get('/formSearch', ['as' => 'search', 'uses' => 'FormController@formSearch']);

Route::resource('form.comments', 'CommentsController');

其中显示注释(并且试图从中调用该方法)的表单视图文件设置为:

<div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">Comments</h4>
    </div>
    <div class="panel-collapse" id="comments" data-context="5" aria-expanded="false">
      <div class="panel-body">
        @forelse ($form->comments as $comment)
          <div class="form-group">
            Comment By: <strong>{{ $comment->user_name }}</strong> at <strong>{{ $comment->created_at }}</strong>
          </div>
          <div class="form-group col-md-10">
            <div class="form-control" readonly>{{ $comment->comment }}</div>
          </div>
          <div class="btn-group col-md-2">
            <form action="{{ route('form.comments.destroy', $comment->id) }}" method="POST">
              {{ csrf_field() }}
              {{ method_field('DELETE') }}
              <button class="btn btn-danger" type="submit" data-confirm="Are you sure you want to delete this comment?" value="delete">
                <i class="fa fa-trash"></i>&nbsp;Delete
              </button>
            </form>
          </div>
        @empty
          <div class="form-group">
            <div class="form-control" readonly>No Comments Found</div>
          </div>
        @endforelse
        <div class="form-group">
          <label class="control-label">
            <a href="#">Back To Top</a>
          </label>
        </div>
      </div>
    </div>
  </div>

最后,在控制器中,方法设置为:

public function destroy($id){
$comment = Comment::find($id);
$comment->delete();

// Redirect
return redirect()->back()->with('status', 'Comment successfully deleted');
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我建议您将comments路由重命名为form/{form}/comments/{comment},因为您获得了路径form,因此需要2个参数。 comment$comment->id。您仅发送1(// Route::resource('form.comments', 'CommentsController'); Route::resource('comments', 'CommentsController'); <form action="{{ route('comments.destroy', $comment->id) }}" method="POST">...

endpoints.get_current_user()