show some view from laravel method [closed]

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:


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")

    $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:
      to pass them as arguments of
      method on next line
    return $this->showQuestion($tester, $testerAnswers, $user);


@foreach($question->items AS $item)

    <div class="item border border-secondary rounded" onclick="redirect('{{$item->id}}')" >
        <img  class="img-fluid" src="{{{$item->image}}}" alt="">
        <p class="d">{{$item->item_text}}</p>


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).

Browse More Popular Posts

Leave a Comment