Laravel错误"缺少Route"所需的参数;当我使用带有foreach循环的表单时

时间:2018-03-29 18:06:02

标签: laravel foreach eloquent

  

缺少[Route:templates.answers.store] [URI:templates / {template} / answers]的必需参数。 (查看:D:\ Applications \ xampp \ htdocs \ clientpad \ resources \ views \ templates \ answers.blade.php)

当我尝试使用带有foreach循环的表单时,我遇到了上述错误。我甚至不确定为什么会这样,也许是因为我是Laravel的新人。但是,一旦我从Eloquent表单中删除了AnswerController @ store,这个错误就消失了。我可能做错了这整个形式。

以下是我想要做的事情:用户制作了一个带有问题的模板,点击使用按钮进入此网址:http://clientpad.test/templates/ {id} / answers他们看到他们提出的问题与一个foreach循环。在它周围制作一个表格,以便用户可以回答所提出的问题。表单和答案字段显示我删除动作AnswerController @ store时,否则我收到上述错误。

以下是代码:

AnswerController:

    public function index(Template $template, Question $question)
        {
            $questions = $template->questions->mapWithKeys(function($question){
                return [$question->id => $question->question];
            });
            return view('templates.answers')->with('template', $template)->with('questions',$questions);
        }

   public function store(Request $request, Question $question, Answer $answer)
    {
        $answers = new Answer;
        $answers->answer = $request->input('answer');
        $answers->question_id = $request->input('question_id'); //current template id
        $question->answers()->save($answers);
        dd($question);
        return redirect('/dashboard')->with('success', 'Your Question Was Successfully Created');
    }

answers.blade.php

                {!! Form::open(['action' => 'AnswersController@store', 'method' => 'POST']) !!}               
                      @foreach ($questions as $question) <!-- Index counts the questions shown -->

                      <div class="panel panel-default">
                          <div class="panel-body">
                              <p class="pull-left question2"> {{$question}}</p>
                              <div class="form-group answer">

                            {{Form::label('', '')}}
                            {{Form::text('answer', '', ['class' => 'form-control', 'placeholder' => 'Type in your answer'])}}
                                </div>
                          </div>
                        </div>

                    @endforeach
                    <hr>
                         {{Form::submit('Save', ['class'=>'btn btn-primary'])}}
                    {!! Form::close() !!}     

我只是在路线中使用资源。

1 个答案:

答案 0 :(得分:1)

您在Blade视图中调用的路由templates/{id}/answers缺少{id}参数。彻底阅读错误将有助于您理解。

而不是写作:

Form::open(['action' => 'AnswersController@store', 'method' => 'POST'])

你写道:

Form::open(['action' => ['AnswersController@store', $template_id], 'method' => 'POST'])

$template_id将填充路线网址{id}中的templates/{id}/answers