Laravel 5.1:表单模型绑定 - 单选按钮

时间:2016-01-17 08:11:40

标签: php forms data-binding radio-button laravel-5.1

情景:

我想制作类似提问者的东西,所以我在一个页面中有多个表单,每个表单都与问题相关,并且有自己的提交按钮,每个问题有4个答案,因此用户只能使用单选按钮为问题选择一个答案。 我需要使用表单数据绑定将数据绑定到相关字段,所有其他数据完全绑定但单选按钮上的问题。

控制器

/* get all answers that related to the topic */
$question_answers = ['criteria_id' => $criteriaId, 'sector_id' => $sector_id];
        $answers = Answer::where($question_answers)->get();
        $questions = $criteria->question()->get();
/* check existing data */
        $check_data = ['organisation_id' => $organisation_id, 'customize_criteria_id' => $criteriaId];
        $exist_data = RatingDatum::where($check_data)->get();

查看:

 @foreach($question as $index=>$question)
                                <div class="plan bg-plan">
                                    <h5>{{$question->question}}</h5>
                                    <hr>
    @foreach($exist_data as $datum)
                                            @if($datum->question_id == $question->id)
                                                {!! Form::model($datum,['method' => 'PATCH','action'=>['RatingDataController@update'],$datum->id]) !!}
                                            @endif
                                        @endforeach
                                        @foreach($answers as $answer)
                                            @if($answer->question_id == $question->id)
                                                <div class="radio">
                                                    <label>

                                                        {!! Form::radio('answer_id['.$index.']', $answer->id, $datum->answer_id != null ? 'true' : 'false')!!}
                                                    {{$answer->score}} {{$answer->answer}}
                                                    </label>
                                                </div>
                                            @endif
                                        @endforeach
                                        <div class="form-group">
                                            {!! Form::label('comment','Comment :') !!}
                                            {!! Form::textarea('comment' ,null,['class'=>'form-control', 'rows' => 4]) !!}

                                            {!! Form::label('reference','Reference:') !!}
                                            {!! Form::textarea('reference',null,['class'=>'form-control', 'rows' => 1]) !!}
                                        </div>
                                        {!! Form::submit('Submit Data', ['class' => 'btn btn-success submit']) !!}
                                        {!! Form::close() !!}
                                                <!--/.Plan -->
                                    </div>

0 个答案:

没有答案