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

MahmoudMonem's avatar

How to Eager Load the queries inside the "show" page

Hello Guys, I'm very new to optimization and I'm trying to improve my code efficiency a bit. Now I actually improved the index pages for so many modules like for example here in courses controller index function

changed this

$courses = Course::orderBy('created_at','desc')->paginate(39);
        return view('courses', ['courses' => $courses]

to this and with this logic I managed to reduce the number of queries a lot for so many other things related to the course.

$courses = Course::with('category')
 ->orderBy('created_at','desc')->paginate(39);
        return view('courses', ['courses' => $courses])

Now I have show page which is [ courses/show ] and simply it has this

public function show(Course $course)
    {
$instructorname = $course->instructor->user->name;
return view('courses.show', ['course' => $course])
}

However in the show view, I am calling " list of students who are enrolled " [ which led me to have many duplicates for select * from users .. ] I am also having course units and units lessons. so I also have many duplicates for select*from units and select * lessons.

How can I eager load these queries in this show page. will appreciate ur inputs :)

0 likes
5 replies
rodrigo.pedra's avatar
Level 56

Actually as in your show(...) method you already have a Course instance you will need to "lazy" eager load these relations:

public function show(Course $course)
{
    $course->load(['instructor.user', 'lessons.units', 'students.user']);

    return view('courses.show', ['course' => $course])
}

Reference: https://laravel.com/docs/8.x/eloquent-relationships#lazy-eager-loading

I am guessing most of the relation names. Also I removed the $instructorname assignment as you didn't use that variable anywhere.

Hope it helps.

1 like
rodrigo.pedra's avatar

It is hard to assess why this is happening without seeing some code.

For example, is this repeated queries when rendering the courses.show view? if so , can you share it? At least the relevant parts.

Also, is your controller method, that renders that view, just lazy eager loading relations and returning the view? If it is doing more stuff, can you share that?

Lastly, can you share the actual relation definition methods from the relevant models?

One more thing: by your description try lazy-eager loading the users relation also:

public function show(Course $course)
{
    $course->load([
        'users', // added
        'instructor.user',
        'lessons.units', 
        'students.user',
    ]);

    return view('courses.show', ['course' => $course])
}

I suspect that instead of just users you can try lazy eager loading users.instructor.user. But it is hard to tell without seeing some more code.

Hope it helps.

1 like
MahmoudMonem's avatar

I just loaded

$course->load(['units.lessons','reviews.user','users' ]);

and that pretty much cleaned the queries from 31 to 15 .. and for some reason the repeated code above is gone and now everything should be ok. I updated my answer for that reason. Thank you @rodrigo.pedra so much for your time <3

1 like

Please or to participate in this conversation.