我有2个表格-问题和答案。我想将每个答案的question_ids插入响应表(调查系统)中。我已经能够在答案表中插入答案,但不能插入相应的question_id。这是我的控制器。
public function store(Request $request)
{
for($i=1; $i<=count($request->answer); $i++)
{
$answers[] = [
'answer' => $request->answer[$i],
'question_id' => $request->question_id];
}
Response::insert($answers);
}
//查看
<div class="form">
<div id="successmessage">Survey sent successful. Thank you!</div>
<div id="errormessage">Errors</div>
<form action="{{ route('surveys.store') }}" method="post" role="form" class="surveyForm">
@csrf
<?php
;$count=1;
?>
@foreach($survey->questions as $question)
@switch($question->input_type)
@case('text')
@case('email')
<div class="form-group">
<input type="{{$question->input_type}}" name="answer[{{$count}}]" class="form-control"
id="{{strtolower($question->qtn)}}" placeholder="{{$question->qtn}}"
data-rule="{{$question->data_rule}}" data-msg="{{$question->data_message}}" />
<div class="validation"></div>
</div>
@break
@case('textarea')
<div class="form-group">
<textarea class="form-control" name="answer[{{$count}}]" rows="5" data-rule="{{$question->data_rule}}" data-msg="{{$question->data_message}}" placeholder="{{$question->qtn}}"></textarea>
<div class="validation"></div>
</div>
@break
@endswitch
<?php $count++; ?>
@endforeach
<div class="text-center">
<button type="submit">Send</button>
</div>
</form>
</div>