mercredi 30 décembre 2015

Laravel: Form Data binding

Scenario:

I have a form for each subject and each subject have three or four questions and the user have to answer those question then save the data on the database

The problem:

How to bind the data with the form so the form refilled with the data that inputted before by the user. I have tried a lot and search a lot to find a solution but I didn't find anything useful.

Code:

Controller:

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';
    }

View :

   <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>



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1YSpn2r
via IFTTT

Aucun commentaire:

Enregistrer un commentaire