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

prospeak's avatar

Radio multi question

I want to save radios group in database with different question_id

I tried to use form with radio's input have name=answer

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();
          $newAnswer->answer = request()->get('answer');
          foreach ($survey->questions as $key=>$question){
            $newAnswer->question_id = $question->id;
          }
          $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 :

<div id="Create" class="col-12" style="display:none;">             
         {!! Form::open(array('action'=>array('AnswerController@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="form-group" style="margin-left: 20px;">
                        <input name="answer[{{$question->id}}]" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
                        <label class="custom-control-label" for="{{ $value }}">{{ $value }}</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() !!}
    
                 </div>

Tables : i have already made it :

  • Answer table = id, survey_id , question_id , answer . commentaire , timestamp
  • Survey table : id title ...
  • question table : id , suvey_id , option_type , options

I got in my database a array: ["question_id" = "value"] what a want is for example i have 2 questions , in my database i want to save 2 row with some survey id but different question id

for example i have 1 survey with 2 questions i want to get in my db 2 lines with same survey id and question_id is question's id and answer id different

could you help me to do that ?

0 likes
4 replies
prospeak's avatar

Is there anyone can help me to fix that ?

Snapey's avatar

Its just too much

You need to break it down to what works and what does not. Just limit the problem.

One thing for sure, this will never work

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

I don't know what you were hoping to achieve, but you just keep overwriting the same question_id field each time around the loop.

Snapey's avatar

Please format your code by putting 3 backticks ``` on a line before and after each code block

prospeak's avatar

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 ?

Please or to participate in this conversation.