如何使用 'hasManyThrough'在 Laravel 中有超过 3 张桌子? [英] How to use 'hasManyThrough' with more than 3 tables in Laravel?

查看:40
本文介绍了如何使用 'hasManyThrough'在 Laravel 中有超过 3 张桌子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档中所述,当前hasManyThrough 可以在你有 country > 之类的东西时使用.用户 >帖子

结果类似于 Country::whereName('xx')->posts;这很棒,但如果我有更多的东西怎么办

<代码>国家>城市 >用户 >帖子甚至

<代码>国家>城市 >城镇 >用户 >帖子

然后你将如何实现类似的东西,以便你可以编写与上面相同的内容

Country::whereName('xx')->posts;Town::whereName('xx')->posts;

解决方案

遗憾的是没有一个简单的解决方案,所以这是我发现的

1- 将另一个表的外来 id 添加到 post 表并坚持 hasMany &属于,例如

帖子id - 整数country_id - 外国//等等...标题 - 字符串

2- 在每个表上使用 hasManyThrough,除了 user &post 因为没有必要,除非你想更深入,例如.

国家id - 整数名称 - 字符串城市id - 整数country_id - 外国名称 - 字符串城市id - 整数city_id - 外国名称 - 字符串用户id - 整数town_id - 外国名称 - 字符串帖子id - 整数user_id - 外国标题 - 字符串

<块引用>

- 所以让我们尝试第二个选项

1- 设置您的 hasMany &属于关系照常

2- 在模型上设置 hasManyThrough.

3- 为了实现上面的 Country::whereName('xx')->posts 示例,我们向 country 模型添加了另一个方法

//因为 Country::find($id)->towns,返回一个数组//所以为了得到所有的帖子,我们必须遍历它//并返回一个包含所有国家/地区帖子的新数组公共功能帖子(){$posts = [];foreach ($this->towns as $town) {$posts[] = $town->posts;}返回 $posts;}

4- 因为整个事情都是通过 foreign_id 进行的,所以我们通过 id 而不是 进行搜索名称Country::find($id)->posts()

as stated in the docs the current hasManyThrough can be used when u have something like country > users > posts

which result in something like Country::whereName('xx')->posts; which is great but what if i have more than that like

country > cities > users > posts or even

country > cities > towns > users > posts

how would you then implement something like that so you can write the same as above

Country::whereName('xx')->posts; or Town::whereName('xx')->posts;

解决方案

sadly there is no one easy solution, so heres what i found

1- add the other table foreign id into the post table and stick to the hasMany & belongsTo, ex.

posts
    id - integer
    country_id - foreign
    // etc...
    title - string

2- go with the hasManyThrough on each table except user & post as there is no need unless you want to go deeper, ex.

countries
    id - integer
    name - string

cities
    id - integer
    country_id - foreign
    name - string

towns
    id - integer
    city_id - foreign
    name - string

users
    id - integer
    town_id - foreign
    name - string

posts
    id - integer
    user_id - foreign
    title - string

- so lets try with the 2nd option

1- set up your hasMany & belongsTo relations as usual

2- setup the hasManyThrough on the models.

3- to achieve the above example of Country::whereName('xx')->posts we add another method to the country model

// because Country::find($id)->towns, return an array
// so to get all the posts we have to loop over that
// and return a new array with all the country posts
public function posts()
{
    $posts = [];

    foreach ($this->towns as $town) {
        $posts[] = $town->posts;
    }

    return $posts;
}

4- as the whole thing is working through the foreign_id so we search by the id instead of the name like Country::find($id)->posts()

这篇关于如何使用 &amp;#39;hasManyThrough&amp;#39;在 Laravel 中有超过 3 张桌子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆