简体   繁体   English

Laravel:如何急切加载一个两步下来的模型?

[英]Laravel: how to eager load a model that's two steps down?

Let's say I have users which have many computers which belong to a certain computer_type (users->computers->computer_type). 假设我的用户拥有许多属于某个computer_type的计算机(users-> computers-> computer_type)。

I know that I can load both users and their computers with: User::with("Computer") 我知道我可以加载用户和他们的计算机: User::with("Computer")

I would like to load all three. 我想加载这三个。 How can I do it in Laravel? 我怎么能在Laravel做到这一点?

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. 用户has_one计算机has_one在此特定方案中键入。

Try 尝试

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

if each kid has a appointment and each kid has a parent. 如果每个孩子都有预约,每个孩子都有父母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM