Laravel:如何绑定表单数据

时间:2015-12-29 22:39:48

标签: php forms data-binding laravel-5

情境:

我有一个每个科目的表格,每个科目有三到四个问题,用户必须回答这些问题,然后将数据保存在数据库中

问题:

如何将数据与表单绑定,以便表单重新填充用户之前输入的数据。 我已经尝试了很多并且经常搜索以找到解决方案,但我找不到任何有用的东西。

代码:

控制器:

public function saveData(Request $request, $user_id, $subject_id)
    {
        $all_data = $request->input('row');
        foreach ($all_data as $data) {
            $question_id = $data['question_id'];
            $check_data = ['user_id' => $user_id, 'question_id' => $question_id, 'subject_id' => $subject_id];
            $exist_data = RatingDatum::where($check_data)->get()->first();
            if (is_null($exist_data)) {
                RatingDatum::create($data);
            } else {
                $save = RatingDatum::findorfail($exist_data->id);
                $save->update($data);
            }
        }
        flash()->success('Data has been submitted.');
        return 'done';
    }

查看:

   <div class="row">
                {!! Form::model(['method' => 'PATCH','action'=>['RatingDataController@saveData'],$organisation->id,$organisation->sector->id]) !!}
           <div class=" col-md-9">
                            <h3> {{$subject_name->subject}}</h3>
                            @foreach($question as $index=>$question)
                                <div class="plan bg-plan">
                                    <h5>{{$question->question}}</h5>
                                    <hr>
                                    <!-- create sector form -->
                                    @foreach($answers as $answer)
                                            <div class="radio">
                                                <label> 
                             {!! Form::radio('row['.$index.'][answer_id]', $answer->id,true)!!}                                                        
                                                  {{$answer->answer}}
                                                </label>

                                            </div>
                                    @endforeach
                                    <div class="form-group">
                                        {!! Form::label('comment','Comment :') !!}
                                        {!! Form::textarea('row['.$index.'][comment]' ,null,['class'=>'form-control', 'rows' => 4]) !!}
                                    </div>                             
                                </div>
                            @endforeach
                        </div>
                        {!! Form::submit('Submit Data', ['class' => 'btn btn-success submit right']) !!}

                        {!! Form::close() !!}
                    </div>

0 个答案:

没有答案