-6

I want to show my content in view but it doesn't show anything


public function myMethod(Request $request)
{
    $this->showQuestion()
}





public function showQuestion($tester, $testerAnswers, $user)
    {
        $testerAnswerNum = $testerAnswers->count();

        $questionIdsAnswered = $testerAnswers->pluck('question_id');
        $question = UserAnswer::where("user_id", $tester->user_id)->whereNotIn('question_id', $questionIdsAnswered)->orderBy("created_at", "desc")->first();


        $question->question->items;

        $questionNum = UserAnswer::where("user_id", $tester->user_id)->count();
        $userName = $user->name;


        return view('create_answer.answer_a_question', compact('question', 'testerAnswerNum', 'questionNum', 'userName'));
    }

and my view file (create_answer.answer_a_question.blade.php):

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>پاسخ به سوالات</title>

    <style>
        .item:hover{
            background: #8BC34A;
        }


        .item{
            background: #eeeeee;
            float: right;
            width: 40%;
            margin: 10px;
            max-width: 200px;
        }
        button {
            background-color: Transparent;
            background-repeat:no-repeat;
            border: none;
            cursor:pointer;
            overflow: hidden;
            outline:none;
        }
        p{
            text-align: center;
            margin: 10px;
        }
        img{
            max-width: 150px;
            max-height: 150px;
            display: block;
            margin-left: 0.1px;

        }

        button.selected{
            background:red;
        }
    </style>
</head>
<body>




<p>{{$testerAnswerNum+1}}/$questionNum}}</p>

<p>{{$userName." ".$question->tester_question_text}}</p>





@for($i=0;$i<sizeof($question->items);$i++)
    @php

        $item=$question->items[$i];

    @endphp
    <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>
    </div>

@endfor
<hr >
<br>
<br>
<button onclick="reload()"  style="background: #ffff;color: #3f9ae5;size: 10px;text-align: center">یک سوال دیگر</button>

<form  id="form" action="{{route('store_question')}}" method="post">
    @csrf
    <input  type="hidden" name="user_id" value="{{$user->id}}">
    <input  type="hidden" name="question_id" value="{{$question->id}}">
    <input  type="hidden" id="item_id" name="item_id"  >

</form>


</body>
</html>

and my UserAnswer file :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserAnswer extends Model
{


    public function question()
    {
        return $this->belongsTo(question::class);
    }

}

my question model:


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class question extends Model
{


    public function items()
    {
        return $this->hasMany(Item::class);
    }
}
13
  • 2
    I would check the file structure... Check the extension of your view file (that should be .blade.php) that has to be in resources/views/create_answer Jun 15, 2019 at 18:05
  • 1
    View should be like resources/views/create_answer/answer_a_question.blade.php Jun 15, 2019 at 18:10
  • 2
    @ceejayoz and another user both removed material in this question that was added to defeat the question quality filter, and it was restored twice. Please don't do this - readability is important here. Thanks!
    – halfer
    Jun 15, 2019 at 18:32
  • 1
    (The question quality filter is not perfect, but if you find you cannot post a question, add some more description text - there is always more material you can add.)
    – halfer
    Jun 15, 2019 at 18:36
  • 3
    Do not edit the question to invalidate existing answers.
    – user3956566
    Jun 16, 2019 at 21:41

2 Answers 2

3

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="{{{$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).

3
  • can you tell me what was the problem, please? Jun 15, 2019 at 18:30
  • @AliGhafari it's just a guess, read my updated answer
    – num8er
    Jun 15, 2019 at 18:31
  • thanks for helping I solved the problem, you can see my own answer Jun 15, 2019 at 19:31
-1

I found the problem! You are calling the function but not returning anything.

In above issue the showQuestion() method return view for myMethod() and myMethod() don't use it, it will be work with add return in myMethod() like this:

public function myMethod(Request $request)
{
    $this->showQuestion()
}

should change to:

public function myMethod(Request $request)
{
     return $this->showQuestion();
}
1
  • This could do with some indentation and, critically, semi-colons and dollar-symbols.
    – halfer
    Jun 15, 2019 at 21:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.