在我的情况下,我必须逐个获取问题并使用各自的选项并将选项ID保存到答案表中,我可以逐个提取问题,但无法将选项ID保存到答案表中
这是我的控制器
public function getQuestion(Request $request,$qId)
{
$questions = Question::find($qId);
$options = Question::find($qId)->options;
$previous = Question::where('id', '<', $questions->id)->max('id');
$next = Question::where('id', '>', $questions->id)->min('id');
return view('Pages/User/Questions/Question2')
->with('options',$options)
->with('questions',$questions)
->with('previous',Question::find($previous))
->with('next',Question::find($next));
}
我的路线
Route::get('/question/{qId}', [
'uses' => 'customers\QuestionController@getQuestion',
'as' => 'question'
]);
我的观点
@foreach
<h3>{{$questions->question_name}}</h3>
@foreach($options as $option)
<div class="checkbox">
<label><input type="checkbox" name="option">{{$option->option}}</label>
@endforeach
@endforeach
@if($previous)
<a href="{{route('question',['qId' => $previous->id])}}">Previous</a>
@endif
@if($next)
<a href="{{route('question',['qId' => $next->id])}}" >Next</a>
@endif
这就是代码看起来像我可以逐行记录记录而不能但是能够在answer表中保存选项id,因为它没有表格。
答案 0 :(得分:0)
你应该改变这样的观点:
@foreach(put here main foreach loop)
<h3>{{$questions->question_name}}</h3>
<form action="storeRoute">
@foreach($options as $option)
<div class="checkbox">
<label><input type="checkbox" name="option[]" value="{{ $option->id }}">{{$option->option}}</label>
@endforeach
<button type="submit">Submit</button>
</form>
@endforeach
@if($previous)
<a href="{{route('question',['qId' => $previous->id])}}">Previous</a>
@endif
@if($next)
<a href="{{route('question',['qId' => $next->id])}}" >Next</a>
@endif
这样选择的选项id作为选项数组发送到storeRoute
您可以在控制器中使用foreach来获取选定的操作值
如果您希望逐个获取所有选定的选项,请使用以下代码:
foreach ($request->option as $opt){
//code to store $opt as question answer
}
或者如果您可以保存所有选定的选项$request->option