我创建了一个选择题,并且能够将它们存储到数据库中,当用户选择正确的答案时,我需要它存储问题的ID和答案。请任何可以帮助我的人
这是我创建问题的表格
@foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt{{ $qn->id }}" value="a">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt{{ $qn->id }}" value="b">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt{{ $qn->id }}" value="c">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt{{ $qn->id }}" value="d">{{$qn->opt4}}
</label>
</div>
@endforeach
这是我的控制人
public function store(Request $request){
// dd($request->all());
$this->validate($request,[
'question_id' =>'required|string',
'opt'.$qn->id =>'required|string',
]);
$qn= Answer::create([
'question_id'=>$request['question_id'],
'opt'=>$request['opt'.$qn->id],
]);
}
答案 0 :(得分:0)
像
public function saveAnswers(Request $request)
{
$questions = Questions::all();
foreach($questions as $qns){
$name = "opt".$qns->id;
Answer::create([
'question_id'=>$qns->id,
'answer'=>$request->$name,
]);
}
}