I am new in laravel, I am making quiz application in laravel, I got this issue
@include('inc.header')
<br>
<div class="container">
<div class="row">
<form method="POST" action="{{ url('/store-quiz') }}" class="" id="quiz_module">
{{ csrf_field() }}
@foreach($questions as $question)
@if($id == $question->category_id)
<legend> Quiz Of {{ $question->category->title }}</legend>
<div class="jumbotron">
<input type="hidden" name="user_id" value="{{ auth()->user()->id }}">
<input type="hidden" name="category_id" value="{{ $question->category_id }}">
<input type="hidden" name="question_id" value="{{ $question->id }}">
<h4>
Question {{ $question->id }}
</h4>
<h3>
{{ $question->question }}
</h3><brx`>
<h5>
 <input type="radio" name="user_answer" class="form-check-input" value="{{ $question->option_a }}">{{ $question->option_a }}<br>
 <input type="radio" name="user_answer" class="form-check-input" value="{{ $question->option_b }}">{{ $question->option_b }}<br>
 <input type="radio" name="user_answer" class="form-check-input" value="{{ $question->option_c }}">{{ $question->option_c }}<br>
 <input type="radio" name="user_answer" class="form-check-input" value="{{ $question->option_d }}">{{ $question->option_d }}<br>
</h5>
</div>
@endif
@endforeach
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/1000hz-bootstrap-validator/0.11.5/validator.min.js"></script>
<script type="text/javascript">
$("#quiz_module").click(function(){
var url = $(this).attr("action");
$.ajax({
url: "/store-answer",
type:"POST",
data: $('form').serialize(),
}); //end of ajax
});
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
}
});
</script>
@include('inc.footer')
You can see my output here https://prnt.sc/p4esh1
Here I am getting all records from database.For answer when I click on any radio button, It will store user_id, category_id, question_id and answer in database.
But the problem is that everytime it take last question's question id.
I want to store individual question id, like if I select 2nd question's answer then in database 2nd question's question id save, same for 3rd question.
But here everytime it saves 3.
How can I resolve this??
Please guide me, Thank You!
In My controller I code like below
public function storeAnswer(Request $request)
{
$create = Quiz::create($request->all());
return redirect('/home');
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…