gpt4 book ai didi

php - 在 Laravel 5.2 中访问多个链接表中的所有数据

转载 作者:行者123 更新时间:2023-11-29 01:52:34 25 4
gpt4 key购买 nike

enter image description here

现在,在所有 4 个模型中,我都提供了 hasmany()belongsTo() 关系。

当我尝试使用以下代码从表中访问特定城市及其关联状态时,它工作正常。

 $citydetails=City_table::with('states')->find($id);

同时我还想访问该州的相关国家/地区名称。我试过这样做:

 $citydetails=City_table::with('states','countries')->find($id);

这给了我一条错误消息,我知道这种方式行不通,因为城市和国家/地区彼此没有直接关系,但我需要像这样访问数据,如果我访问特定城市,那么我希望它的状态为以及结果中的国家/地区。

最佳答案

您可以使用点符号来访问嵌套关系。引用Laravel Documentation .

To eager load nested relationships, you may use "dot" syntax. For example, let's eager load all of the book's authors and all of the author's personal contacts in one Eloquent statement:

$books = App\Book::with('author.contacts')->get();

在您的情况下,这看起来像:

 $citydetails = City_table::with('states.countries')->find($id);

关于php - 在 Laravel 5.2 中访问多个链接表中的所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37715839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com