You were trying to “hop” over question
relation to get items
.
So just pick question_id
of one “unanswered answer” and then get question using with('items')
.
Also it’s unclear how You get $tester, $testerAnswers, $user
.
So below are ideas how to fix Your issues:
Action:
public function showQuestion($tester, $testerAnswers, $user)
{
$testerAnswerNum = $testerAnswers->count();
$questionIdsAnswered = $testerAnswers->pluck('question_id');
$unansweredOne = UserAnswer::where("user_id", $tester->user_id)
->whereNotIn('question_id', $questionIdsAnswered)
->orderBy("created_at", "desc")
->first(['question_id']);
$question = question::with('items')->find($unansweredOne->question_id);
$questionNum = UserAnswer::where("user_id", $tester->user_id)->count();
$userName = $user->name;
return view('answers.answer_a_question', compact('question', 'testerAnswerNum', 'questionNum', 'userName'));
}
public function myMethod(Request $request)
{
/*
here You get:
$tester
$testerAnswers
$user
to pass them as arguments of
method on next line
*/
return $this->showQuestion($tester, $testerAnswers, $user);
}
View:
@foreach($question->items AS $item)
<div class="item border border-secondary rounded" onclick="redirect('{{$item->id}}')" >
<img class="img-fluid" src="https://stackoverflow.com/questions/56612878/{{{$item->image}}}" alt="">
<p class="d">{{$item->item_text}}</p>
</div>
@endforeach
also change filename
from: create_answer.answer_a_question.blade.php
to: answer_a_question.blade.php
and put it inside of folder: resources/views/answers/
(create it if not exists)
keep in mind for laravel view
helper .
(dot) means /
(directory separator).