Php 如何逐行获取记录并保存其值
在我的情况下,我必须一个接一个地获取问题及其各自的选项,并将选项id保存到答案表中,我可以一个接一个地获取问题,但无法将选项id保存到答案表中 这是我的控制器Php 如何逐行获取记录并保存其值,php,laravel,laravel-5,laravel-5.3,laravel-5.4,Php,Laravel,Laravel 5,Laravel 5.3,Laravel 5.4,在我的情况下,我必须一个接一个地获取问题及其各自的选项,并将选项id保存到答案表中,我可以一个接一个地获取问题,但无法将选项id保存到答案表中 这是我的控制器 public function getQuestion(Request $request,$qId) { $questions = Question::find($qId); $options = Question::find($qId)->options; $previous = Question::whe
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));
}
我的看法
@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
@foreach
{{$questions->question_name}
@foreach($options作为$option)
{{$option->option}
@endforeach
@endforeach
@如果(以前的)
@恩迪夫
@如果($next)
@恩迪夫
这就是代码看起来可以逐行记录,但无法将选项id保存在应答表中,因为它不在表单中。您应该这样更改视图:
@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
或者,如果您可以保存所有选定的选项
$request->option
,您应该像这样更改视图:
@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
或者,如果您可以保存所有选择的选项
$request->option
,那么为什么不使用表单?@Mohammad b我尝试使用表单,但无法逐个获取记录。请对此提供帮助。说明如何将答案存储在数据库中?@Mohammad b这是一个类似于在线测试的多选题,一旦我获取了一个带有选项的问题,我只需要将用户选择的选项Id存储到答案表中,我需要存储问题Id、选项Id(选择的答案),user_id.那你为什么不使用表单?@Mohammad b我尝试使用表单,但无法逐个获取记录。请你对此提供帮助。解释如何将答案存储在数据库中?@Mohammad b这是一个类似于在线测试的多项选择题,一旦我获取了一个带有选项的问题,我只需要将用户选择的选项Id存储到答案表中,我需要存储问题Id、选项Id(选定答案)、用户Id。感谢您的回答,但我需要使用“下一步”(超链接)按钮将选项数组发送到存储路由,是否可能,如果使用“提交”按钮,则会有三个按钮看起来不太好。@ManjunathAM有很多方法可以做到这一点,例如,在控制器中的还原选项返回重定向到下一页url后,或使用jquery提交并转到下一页,……感谢您的回答,但我需要发送选项数组以使用下一个(超链接)按钮存储路由,是否可能,如果使用“提交”按钮,将有三个按钮看起来不太好。@ManjunathAM有很多方法可以做到这一点,例如,在控制器中的还原选项返回重定向到下一页url后,或者使用jquery提交并转到下一页,。。。
foreach ($request->option as $opt){
//code to store $opt as question answer
}