here we go again...
Please refer to my earlier reply
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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 ?
Please or to participate in this conversation.