i’m trying to get user answer for every question that they are answer.
but, the answer for every user is same with the latest user answer.
here is for the controller :
$category_kelas = CategoryKelas::where('id', $category_kelas_id)->with('category', 'kelas')->first();
$question = Question::where('category_id', $category_kelas->category_id)->get();
$category_kelas['users'] = UserQuiz::where('category_id', $category_kelas->category_id)->where('kelas_id', $category_kelas->kelas_id)->with('user')->get();
foreach ($category_kelas->users as $item) {
$item['question'] = $question;
}
foreach ($category_kelas->users as $item) {
foreach ($item->question as $item2) {
$item2['answer'] = UserAnswer::where('user_quiz_id', $item->id)->where('question_id', $item2->id)->first();
}
}
and here is for the blade view :
<tr>
<th>Jenis Tes</th>
<th>Kelas</th>
<th>Peserta</th>
<th>Selesai</th>
@foreach ($category_kelas->users as $index => $item)
@foreach ($item->question as $item2)
@if ($index == 0)
<th>{{ $item2->order_number }}</th>
@endif
@endforeach
@endforeach
</tr>
</thead>
<tbody>
@foreach ($category_kelas->users as $item)
<tr>
<td>{{ $category_kelas->category->name }}</td>
<td>{{ $category_kelas->kelas->name }}</td>
<td>{{ $item->user->name }}</td>
<td>{{ $item->finish }}</td>
@foreach ($item->question as $item2)
<td>{{ $item2->answer->value }}</td>
@endforeach
</tr>
@endforeach
</tbody>
and the result is like this :
the result is using the latest user answerm not its own answer.
please help