Home » Php » php – Laravel: how to eager load a model that's two steps down?

php – Laravel: how to eager load a model that's two steps down?

Posted by: admin July 12, 2020 Leave a comment

Questions:

Let’s say I have users which have many computers which belong to a certain computer_type (users->computers->computer_type).

I know that I can load both users and their computers with: User::with("Computer")

I would like to load all three. How can I do it in Laravel?

How to&Answers:

To retrieve relationships that far down, you must say the child, then the child with it’s child, and so on…

User::with(array('computer', 'computer.type'))->find(1);

User has_one Computer has_one type in this particular scenario.

Answer:

Try

Appointment::with('kid.parent')->get()

if each kid has a appointment and each kid has a parent.