本文介紹了在 Laravel 中使用 Eloquent 檢索關系的關系的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個包含以下表格和關系的數據庫:
I have a database with the following tables and relationships:
廣告1-1
汽車m-1
車型m-1
品牌
Advert 1-1
Car m-1
Model m-1
Brand
如果我想檢索一個廣告,我可以簡單地使用:
If I want to retrieve an Advert, I can simply use:
Advert::find(1);
如果我想要汽車的詳細信息,我可以使用:
If I want the details of the car, I could use:
Advert::find(1)->with('Car');
但是,如果我還想要模型的細節(遵循與汽車的關系),語法是什么,以下不起作用:
However, if I also want the detail of the Model (following the relationship with Car), what would the syntax be, the following doesn't work:
Advert::find(1)->with('Car')->with('Model');
非常感謝
推薦答案
在官方 文檔 下急切加載"
It's in the official documentation under "Eager Loading"
多重關系:
$books = Book::with('author', 'publisher')->get();
嵌套關系:
$books = Book::with('author.contacts')->get();
所以對你來說:
Advert::with('Car.Model')->find(1);
這篇關于在 Laravel 中使用 Eloquent 檢索關系的關系的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!