Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

prospeak's avatar

Laravel : save radios group to rows in same page

I have survey site, I want in survey page, display questions with radios option, what to do it save this answers as for each answer saved in line

Database :

answer table: id, survey_id, question_id, answer, commentaire ,timestamp

survey table: id, title ...

question table: id, suvey_id, option_type, options

Controller :

public function store(Request $request, Survey $survey)
    {   
        $question = \App\Question::where('survey_id', '=' , $survey->id)->get();
        $request->validate([
            'answer'=>'required'
          ]);
         $arr = $request->except('_token');
          $newAnswer = new Answer();

          foreach ($survey->questions as $key=>$question){
            $newAnswer->question_id = $question->id;

          $newAnswer->answer = request()->get('answer');
          $newAnswer->user_id = Auth::id();
          $newAnswer->last_ip = request()->ip();
          $newAnswer->survey_id = $survey->id;
          $newAnswer->commentaire = request()->get('commentaire');
          }
          givePoint(new VoteAdded($newAnswer));
          $newAnswer->save(); 

          return redirect()->action('SurveyController@view_survey_answers', [$survey->id]);
    }

View :

{!! Form::open(array('action'=>array('AnswersController@store', $survey->id))) !!}
         <div class="text-center">
         <img class="rounded"
         src="https://interactive-examples.mdn.mozilla.net/media/examples/grapefruit-slice-332-332.jpg"
         alt="Grapefruit slice atop a pile of other slices" />
         </div>
         <hr>
          @foreach ($survey->questions as $question)

            <p class="flow-text">Question {{ $zero++}} - {{ $question->title }}</p>
                @if($question->question_type === 'text')
                <div class="form-group" style="margin-left: 20px;">
                  <div class="input-field col s12">
                    <input id="answer" type="text" name="answer">
                    <label for="answer">Answer</label>
                  </div> 
                </div>
                @elseif($question->question_type === 'textarea')
                <div class="form-group" style="margin-left: 20px;">
                  <div class="input-field col s12">
                    <textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
                    <label for="textarea1">Textarea</label>
                  </div>
                </div>
                @elseif($question->question_type === 'radio')
                  @foreach($question->option_name as $key=>$value)
                    <p style="margin:0px; padding:0px;">
                      @if($value === 'else')
                      <div class="form-group" style="margin-left: 20px;">
                        <input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
                        <label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
                        <div id="textboxes" style="display: none">
                            <br>
                            <textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
                        </div>
                      </div> 
                        @else
                      <p style="margin:0px; padding:0px;">
                        <div class="radio">
                                <label>
                                    <label>
                                        <input type="radio" class="" name="answer[]" value="{{ $value }}"> {{ $value}}
                                    </label>
                                </label>
                            </div>
                    </p>
                        @endif
                  @endforeach
                @elseif($question->question_type === 'checkbox')
                  @foreach($question->option_name as $key=>$value)
                  <p style="margin:0px; padding:0px;">
                      <div class="form-group">

                      <input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
                      <label for="{{$value}}">{{ $value }}</label>
                      </div>
                  </p>
                  @endforeach
                @endif 
              <div class="divider" style="margin:10px 10px;"></div>
              @endforeach
          <div class="form-group">
        {{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
          </div>
        {!! Form::close() !!}

What I want to is: I have survey page with 2 or 3 questions and for each questions there's 3 or 4 radios answers, I want to save answers in my database as for each 1 answer has row not array, could you help me to make that ?

0 likes
10 replies
Snapey's avatar

here we go again...

Please refer to my earlier reply

prospeak's avatar

Should i delete foreach in controller ?

Snapey's avatar

before you do anything else, check what data you are getting from the form for radios.

Radios are always single so you should not be dealing with multiples per question

prospeak's avatar

I want to get only one anwser for each question

jlrdw's avatar

No it's not working

?

  • cache cleared ?
  • temp views cleared ?
  • did you look at example given, it works ?
prospeak's avatar

Yes i did the problem is i have multi question foreach question the user could choose only 1 answer but i want to display in my database foreach answer , a line not array

prospeak's avatar

Is there any way to add next between questions and submit in the end of form ?

jlrdw's avatar

the problem is i have multi question foreach question the user could choose only 1 answer but i want to display in my database foreach answer

Before proceeding you really need to take some basic examples somewhere of how a radio is used and the data is stored and later the data is retrieved.

Honest radios are not hard at all and there are probably tens of thousands of free examples all over the web.

Please or to participate in this conversation.