Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

oma baz's avatar

stuck in load() and compact')

i try to get questions relative to the chosen subject but when i acces to a subject i get all question "each certifact contains some subjects and each subject contains questions"

this is my controller

{public function index(Subject $subject) {

$questions = question::with('subjects')->get();

//$questions->load('subjects'); 
$subject->load('certificats', 'subjectsQuestions');

return view('admin.quizz.index' , compact('questions','subject'));

}

0 likes
36 replies
Sinnbeck's avatar

This line just gets every single question. Remove it. Your questions are loaded below it (I assume subjectsQuestions are questions. If not, load them there )

$questions = question::with('subjects')->get();
2 likes
Sinnbeck's avatar

@oma baz yes naturally. You removed it. Remove it from compact as well. In blade do this

@foreach ($section->subjectsQuestions as $question)
     
@endforeach 

But judging by the rest of the thread, your logic needs some rewriting for this to work

1 like
Sinnbeck's avatar

@oma baz Did you fix eveything that tykus explained to you?

tykus's avatar
tykus
Best Answer
Level 104

You have a Subject instance, presumably with a questions relation, so:

public function index(Subject $subject)
{
    $questions = $subject->questions;

    return view('admin.quizz.index' , compact('questions'));
}
2 likes
oma baz's avatar

@tykus when i do this i get this error count(): Parameter must be an array or an object that implements Countable (View:C:\laragon\www\dev-ramram-3af3e537ee66e4f5 (1)\resources\views\admin\quizz\index.blade.php)

@extends('layouts.admin')

@section('content')

        <div class="row">

            <div class="col-md-12">


                    <form  method="post">

                        @csrf



                    @if(count($questions) > 0)
tykus's avatar

@oma baz do you have a questions relationship on the Subject model; if not, then the $subject->questions will be null!?!?!?

1 like
oma baz's avatar

@tykus i have this inside my subjects model

public function subjectsQuestions() { return $this->hasMany(Question::class, 'subjects_id', 'id'); }

tykus's avatar

@oma baz well in that case:

public function index(Subject $subject)
{
    $questions = $subject->subjectsQuestions;

    return view('admin.quizz.index' , compact('questions'));
}
1 like
oma baz's avatar

@tykus @tykus when i do this i get this error count(): Parameter must be an array or an object that implements Countable (View:C:\laragon\www\dev-ramram-3af3e537ee66e4f5 (1)\resources\views\admin\quizz\index.blade.php)

@extends('layouts.admin')

@section('content')

        <div class="row">

            <div class="col-md-12">


                    <form  method="post">

                        @csrf



                    @if(count($questions) > 0)
tykus's avatar

@oma baz subjectsQuestions, not subjectQuestions above.

Your naming is non-conventional; check yourself for typos!

1 like
tykus's avatar

@oma baz in the index action:

$questions = $subject->subjectsQuestions;
1 like
oma baz's avatar

@tykus ah okey thank youu i did this but in the view i got just the layouts no information inside this is my index:

@extends('layouts.admin') @section('content')

        <div class="row">
            <div class="col-md-12">
                    
                    <form  method="post">
                        @csrf
                       
                    
                    @if(count($questions) > 0)
                        <div class="panel-body">
                        <?php $i = 1; ?>
                        @foreach($questions as $question)
                            @if ($i > 1) <hr /> 
                            @endif
                            <div class="row">
                                <div class="col-xs-12 form-group">
                                    <div class="form-group">
                                        <h3 style="margin-left:5%" ><strong>Question {{ $i }}.{{ $question->question }}</strong></h3>
                
                                        <ul class="list-group" style="margin-left:5% ; margin-right:5%">
                                            <div class="row ">
                                                
                                                    <li class="list-group-item">
                                                        <div class="form-check">
                                                            <input class="form-check-input ml-2" type="radio" name="selected_answer" id="option_1" value="1">
                                                            <label class="form-check-label text-dark" for="option_1">
                                                                {{ $question->option_a }}
                                                            </label>
                                                        </div>
                                                    </li>
                                                </div>
                                                <br>
                                                <div class="row ">
                                                    <li class="list-group-item">
                                                        <div class="form-check">
                                                            <input class="form-check-input ml-2" type="radio" name="selected_answer" id="option_2" value="2">
                                                            <label class="form-check-label text-dark" for="option_2">
                                                                {{ $question->option_b }}
                                                            </label>
                                                        </div>
                                                    </li>
                                                </div>
                                            <br>
                                            <div class="row ">
                                                
                                                    <li class="list-group-item">
                                                        <div class="form-check">
                                                            <input class="form-check-input ml-2" type="radio" name="selected_answer" id="option_3" value="3">
                                                            <label class="form-check-label text-dark" for="option_3">
                                                                {{ $question->option_c }}
                                                            </label>
                                                        </div>
                                                    </li>
                                                </div>
                                                <br>
                                                <div class="row">
                                                    <li class="list-group-item">
                                                        <div class="form-check">
                                                            <input class="form-check-input ml-2" type="radio" name="selected_answer" id="option_4" value="4">
                                                            <label class="form-check-label text-dark" for="option_4">
                                                                {{ $question->option_d }}
                                                            </label>
                                                        </div>
                                                    </li>
                                                </div>
                                            
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <?php $i++; ?>
                            
                            <button type="submit" id="submit" class="btn btn-primary btn-sm pull-right  " style="margin-right: 5%">Submit</button>
                          @endforeach
                        </div>
                    @endif
                  
                    </form>
            </div>
        </div>
    </div>
    

@endsection

@section('scripts')

@endsection

@section('styles')

@endsection

tykus's avatar

@oma baz

ah okey thank youu i did this but in the view i got just the layouts no information inside this is my index:

In that case, there are no questions associated with the given Subject instance

tykus's avatar

@oma baz try debugging:

public function index(Subject $subject)
{
    $questions = dd($subject->subjectsQuestions);

    return view('admin.quizz.index' , compact('questions'));
}

Do you see a Collection with Question instances?

oma baz's avatar

@tykus this is what i get

Illuminate\Database\Eloquent\Collection {#1494 ▼ #items: [] #escapeWhenCastingToString: false }

oma baz's avatar

@tykus but with the last code i got the correct view ,the problem is that i want only questions for the subjeect chosen not all subjects, i mean with this code

class QuizzController extends Controller {

    public function index(Subject $subject){
    
    $questions = question::with('subjects')->get();
   
return view('admin.quizz.index' , compact('questions','subject'));

}

tykus's avatar

@oma baz can you please try to format your code correctly?

Anyway, it looks like your Collection has no items? Your relationship is this:

public function subjectsQuestions()
{
    return $this->hasMany(Question::class, 'subjects_id', 'id');
}

so... you have a subjects_id column on the questions table related to the id column on the subjects table?

oma baz's avatar

@tykus yes sir this is my questions table : [ 'question', 'option_a', 'option_b', 'option_c', 'option_d', 'answer', 'details', 'subjects_id', 'created_at', 'updated_at', 'deleted_at', ];

tykus's avatar

@oma baz okay. So, for the given Subjects instance; you have no related Questions; for example, if you changed the query:

$questions = question::where('subjects_id', $subject->id)->get();
dd($questions);

You will see that there are no Questions returned!

oma baz's avatar

@tykus i have the same problem for all subjects : the view returned contains all question not for the subject chosen

tykus's avatar

@oma baz all subjects????

Can you give me (1) the route definition (2) the URL you are visiting and (3) an example of an incorrect question being returned?

oma baz's avatar

@tykus (1) Route::resource('quiz', 'QuizzController'); (2)/admin/certificats/3 "here i have a list of subjects when i click on the wanted subject i go to the following : /admin/quiz?2 (3): i have four subject , i choose the wanted subject but when i click on it all the questions for all the subjects are shown ,but me i want just questions for the chosen subject

tykus's avatar

@oma baz so much useless information here...

What is the Route that handles this Request URL /admin/quiz?2???

oma baz's avatar

@tykus this one :Route::resource('quiz', 'QuizzController') and the url i'm visting is localhost/admin/quiz

tykus's avatar

@oma baz why then does your Index method have a $subject parameter for Route-Model binding????

What does 2 mean here?

oma baz's avatar

@tykus got this [{"id":1,"question":"hahahaa","option_a":"NN","option_b":"NN","option_c":null,"option_d":null,"answer":null,"details":null,"created_at":"2022-01-25 11:42:32","updated_at":"2022-01-25 11:42:32","deleted_at":null,"subjects_id":1,"question_image":[],"subjects":{"id":1,"title":"g\u00e9neralit\u00e9s r\u00e9glemen","created_at":"2022-01-25 11:18:20","updated_at":"2022-01-25 11:18:20","deleted_at":null,"certificats_id":1},"media":[]},{"id":2,"question":"nn","option_a":"hah","option_b":"j","option_c":null,"option_d":null,"answer":null,"details":null,"created_at":"2022-01-25 11:46:41","updated_at":"2022-01-25 11:46:41","deleted_at":null,"subjects_id":1,"question_image":[],"subjects":{"id":1,"title":"g\u00e9neralit\u00e9s r\u00e9glemen","created_at":"2022-01-25 11:18:20","updated_at":"2022-01-25 11:18:20","deleted_at":null,"certificats_id":1},"media":[]},{"id":3,"question":"1-\tSur la figure suivante jointe, le type de contrainte s'appliquant sur le point indiqu\u00e9 sur la figure 3 est :","option_a":"Torsion","option_b":"Traction","option_c":"cisaillement","option_d":"compression","answer":"option_a","details":null,"created_at":"2022-01-26 06:41:33","updated_at":"2022-01-26 06:41:33","deleted_at":null,"subjects_id":2,"question_image":[{"id":1,"model_type":"App\Models\Question","model_id":3,"uuid":"49302f97-140b-4dca-9f58-e3aaf94d7e95","collection_name":"question_image","name":"61f0ecf21c7f7_tech cellule","file_name":"61f0ecf21c7f7_tech-cellule.PNG","mime_type":"image/png","disk":"public","conversions_disk":"public","size":29022,"manipulations":[],"custom_properties":{"generated_conversions":{"thumb":true,"preview":true}},"responsive_images":[],"order_column":1,"created_at":"2022-01-26T06:41:34.000000Z","updated_at":"2022-01-26T06:41:37.000000Z","url":"http://localhost/storage/1/61f0ecf21c7f7_tech-cellule.PNG","thumbnail":"http://localhost/storage/1/conversions/61f0ecf21c7f7_tech-cellule-thumb.jpg","preview":"http://localhost/storage/1/conversions/61f0ecf21c7f7_tech-cellule-preview.jpg"}],"subjects":{"id":2,"title":"Technologie cellule","created_at":"2022-01-26 06:35:42","updated_at":"2022-01-26 06:35:42","deleted_at":null,"certificats_id":3},"media":[{"id":1,"model_type":"App\Models\Question","model_id":3,"uuid":"49302f97-140b-4dca-9f58-e3aaf94d7e95","collection_name":"question_image","name":"61f0ecf21c7f7_tech cellule","file_name":"61f0ecf21c7f7_tech-cellule.PNG","mime_type":"image/png","disk":"public","conversions_disk":"public","size":29022,"manipulations":[],"custom_properties":{"generated_conversions":{"thumb":true,"preview":true}},"responsive_images":[],"order_column":1,"created_at":"2022-01-26T06:41:34.000000Z","updated_at":"2022-01-26T06:41:37.000000Z","url":"http://localhost/storage/1/61f0ecf21c7f7_tech-cellule.PNG","thumbnail":"http://localhost/storage/1/conversions/61f0ecf21c7f7_tech-cellule-thumb.jpg","preview":"http://localhost/storage/1/conversions/61f0ecf21c7f7_tech-cellule-preview.jpg"}]},{"id":4,"question":"2-\tComment peut-on r\u00e9duire le moment fl\u00e9chissant support\u00e9 par les ailes en vol :","option_a":"Installer les moteurs pr\u00e8s du fuselage et maintenir le carburant dans les r\u00e9servoirs ext\u00e9rieurs le plus longtemps possible","option_b":"Cr\u00e9er une d\u00e9flexion des ailerons vers le bas et conserver le carburant dans les r\u00e9serv

oma baz's avatar

@tykus ah i deleted it and i got the result i just posted sorry i'm beginner

oma baz's avatar

@tykus just to tell u that there is items in the subject chosen

tykus's avatar

@oma baz the premise of your original question was all wrong - you have a QuizController and you want to filter by a Subject.

Problem 1: your links are constructed incorrectly; /admin/quiz?2 should be like /admin/quiz?subject=2

Problem 2: you cannot Route-Model bind from a query param. You typehinted Subject $subject on the index action, but this is incorrect - the id is coming from the query param:

public function index()
{
    $subject = Subject::find($request->get('subject'));
    $questions = $subject->questions ?? [];

    return view('admin.quizz.index' , compact('questions'));
}
1 like

Please or to participate in this conversation.