Simple. you create a table where you have the
- question_id
- user_id
- value
So if the user 1 select bad on question 1
1 1 1
Selects Great on question two
2 1 5
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello guys, my structure is like that. I have the following tables tests -> questions -> options -> responses -> results
My test has many questions -> which has many options > there is no right and wrong so i am doing the following in blade
the final question will look something like this
Question1 : How do you like our restaurant
Question Options : Great 🔘 🔘🔘 🔘🔘 Bad
Options weights : Great 5 4 3 2 1 Bad
Question2 : How do you rate our food
Question Options : Great 🔘 🔘🔘 🔘🔘 Bad
Options weights : Great 5 4 3 2 1 Bad
Question3 : How do you rate our ambience
Question Options : Great 🔘 🔘🔘 🔘🔘 Bad
Options weights : Great 5 4 3 2 1 Bad
radio buttons like [ agree/disagree style ] each option has a weight and I would like to capture that in the final result as well
@foreach($test->questions as $question )
{{$question->title}}
@foreach($questions->options as $option
<div class="custom-control custom-option custom-control-inline mb-2 ">
<input class="custom-control-input" type="radio" name="choice-{{$question->id}}" id="radio-{{$option->id}}" value="{{$option->weight}}" required>
<label class="custom-option-label rounded-circle" for="radio-{{$option->id}}">
<span class="custom-option-color rounded-circle" >
</span>
</label>
</div>
@endforeach
@endforeach
what I want to achieve 2 things ; 1- insert into responses table all the options selected [ question ids - selected choices id ] 2- insert into results table the user id - test id - sum of the selected options weights
how the code will look like in controller. I can do simple insert and creations but I am really stuck with this for a while. because I need to insert all data
I got it all sorted out thanks @tray2 and @snapey .. here is the full code for future references
blade view
@foreach($test->questions as $question )
{{$question->title}}
@foreach($questions->options as $option
<div class="custom-control custom-option custom-control-inline mb-2 ">
<input class="custom-control-input" type="radio" name="choice-{{$question->id}}" id="radio-{{$option->id}}" value="{{$option->weight}}" required>
<label class="custom-option-label rounded-circle" for="radio-{{$option->id}}">
<span class="custom-option-color rounded-circle" >
</span>
</label>
</div>
@endforeach
@endforeach
Controller
$test_id= $request->input('test_id');
$weight= $request->input('questions', []);
$test_score = 0;
$answers = [];
foreach ($request->get('questions') as $question_id => $answer_id) {
$question = Question::find($question_id);
$option = Option::where('question_id',$question_id)
->where('id', $answer_id);
$answers[] = [
'question_id' => $question_id,
'option_id' => $answer_id,
'weight' => $weight[$question_id],
];
}
//dd($request->all());
$result = Result::create([
'test_id' => $test_id,
'user_id' => \Auth::id(),
'result' => $test_score
]);
$result->answers()->createMany($answers);
return back()->with('success');
}
Please or to participate in this conversation.