arakibi
arakibi

Reputation: 447

Eager Loading on 3 Models in Laravel 5

I have 3 Models that are related so : "Tutorial" belongs To "Title" that belongs to "Course". Or (the other way) . A "Course" has many "Titles" that have many "Tutorials" . And I want to find a course based on its id and grab all its titles and tutorials using the eager loading . the code looks so :

$course = Course::with('titles')->where('id','=',$id)->get(); 

// this returns only a Course with its titles , but I want to get also tutorials that belongs to each title.

Upvotes: 0

Views: 468

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153150

You can eager load nested relations with the dot-syntax as documented here

$course = Course::with('titles.tutorials')->find($id);

As you can see I also changed the where('id', '=', $id) to find($id). This will do the same but also only return one result instead of a collection.

Upvotes: 2

Related Questions