我有一个包含以下表和关系的数据库:
租房广告1-1 Car m-1型号m-1品牌
如果我想要检索广告,我可以简单地使用:
Advert::find(1);
如果我想要汽车的细节,我可以使用:
Advert::find(1)->with('Car');
但是,如果我还想要模型的细节(跟随与Car的关系),语法是什么,以下不起作用:
Advert::find(1)->with('Car')->with('Model');
非常感谢
解决方法:
它是在“Eager Loading”下的官方documentation
多种关系:
$books = Book::with('author', 'publisher')->get();
嵌套关系:
$books = Book::with('author.contacts')->get();
所以对你来说:
Advert::find(1)->with('Car.Model')->get();