我有一个包含以下表格和关系的数据库:
广告1-1
汽车m-1
型号m-1
品牌
如果我想检索广告,我可以简单地使用:
Advert::find(1);
如果我想要汽车的详细信息,我可以使用:
Advert::find(1)->with('Car');
但是,如果我还想要模型的详细信息(遵循与 Car 的关系),语法是什么,以下不起作用:
Advert::find(1)->with('Car')->with('Model');
非常感谢
最佳答案
在官方documentation在“急切加载”下
多重关系:
$books = Book::with('author', 'publisher')->get();
嵌套关系:
$books = Book::with('author.contacts')->get();
所以对你来说:
Advert::with('Car.Model')->find(1);
关于php - 在 Laravel 中使用 Eloquent 检索关系的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963483/