Level 73
@dalivan the problem is that this code:
<input type="text" name="question_id" value="{{ $row->id }}">
in a loop will always be set to the last item in the collection.
So you might need to do this instead:
<input type="text" name="question_id[$row->id]" value="{{ $row->id }}">
And in the controller:
foreach ((array)$request->answer as $rowId => $value) {
$answer = new Answer();
$answer->user_id = $user_id;
$answer->question_id = $request->question_id[$rowId];
$answer->answer = $value;
$answer->save();
}
OR you could also just use the $rowId here as that's the question ID anyway, so just this should do it as well:
foreach ((array)$request->answer as $rowId => $value) {
$answer = new Answer();
$answer->user_id = $user_id;
$answer->question_id = $rowId;
$answer->answer = $value;
$answer->save();
}
Let me know if it works :)
1 like