Laravel 5中的3个模型关系 [英] 3 models relationship in Laravel 5

查看:71
本文介绍了Laravel 5中的3个模型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个模型.

  • 项目{id,名称}
  • 平台{id,name}
  • 版本{id,value}

一个项目可以具有多个平台,并且每个平台可以在一个项目内具有多个版本.我有与模型相关的下表:

One Project can have multiple Platforms and each Platform can have multiple Versions within a Project. I have the following table which relates the models:

您可以看到Project 6在平台1上具有版本1和3,而Project 8在同一平台上具有版本1和2.

As you can see Project 6 has versions 1 and 3 on platform 1, while Project 8 has versions 1 and 2 on the same platform.

我想获取所有Project的平台以及这些平台的版本,具体取决于Project.

I want to get all Projects' Platforms and versions of those Platforms depending on the Project.

例如,我想做

App\Project::find(6)->platform()->with('version')->get();

在此项目的平台1的版本集合中仅获得ID为1和3的版本.

and get only versions with ids of 1 and 3 on my version collection of platform 1 of this project.

这就是我在项目模型中所拥有的:

This is what I have in Project Model:

public function platform()
{
    return $this->belongsToMany('App\Platform')->withPivot('version_id')->distinct();
}

这就是我在平台模型中拥有的东西:

This is what I have in Platform Model:

public function version()
{
    return $this->belongsToMany('App\Version', 'platform_project')->distinct();
}

您能帮我找出实现所需结果的最佳方法吗?

Could you please help me figure out the best way to achieve the desired result?

推荐答案

在这种情况下,您应该使用

In this case you should use the hasManyThrough relationship.

定义:

具有多次通过"关系为通过中间关系访问远处的关系提供了方便的快捷方式.例如,一个国家(地区)模型可能通过一个中间用户模型具有许多邮政模型.在此示例中,您可以轻松地收集给定国家/地区的所有博客文章.

The "has-many-through" relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Country model might have many Post models through an intermediate User model. In this example, you could easily gather all blog posts for a given country.

尝试一下:

项目模型

 public function versions()
    {
        return $this->hasManyThrough(
            'App\Version',
            'App\Platform',
        );
    }

传递给hasManyThrough方法的第一个参数是我们希望访问的最终模型的名称,而第二个参数是中间模型的名称.

The first argument passed to the hasManyThrough method is the name of the final model we wish to access, while the second argument is the name of the intermediate model.

典型的雄辩外键约定将在执行关系的查询时使用.如果要自定义关系的键,可以将它们作为hasManyThrough方法的第三个和第四个参数传递.第三个参数是中间模型上外键的名称.第四个参数是最终模型上外键的名称.第五个参数是本地键,而第六个参数是中间模型的本地键:

Typical Eloquent foreign key conventions will be used when performing the relationship's queries. If you would like to customize the keys of the relationship, you may pass them as the third and fourth arguments to the hasManyThrough method. The third argument is the name of the foreign key on the intermediate model. The fourth argument is the name of the foreign key on the final model. The fifth argument is the local key, while the sixth argument is the local key of the intermediate model:

因此您可以访问所有项目版本,如下所示:

So you can acess all project version's like this:

$projectVersions = Project::find($id)->versions();

foreach($projectVersions as $version)
{
  echo $version->value; // this will echo each version value from the given project
}

此外,我认为您应该将您的关系更改为 hasMany

Also, i think you should change your relationships to hasMany

项目模型

public function platforms()
{
    return $this->hasMany('App\Platform')->distinct();
}

平台模型


public function versions()
{
    return $this->hasMany('App\Version')->distinct();
}

public function project()
{
    return $this->belongsTo('App\Project');
}

版本模型

public function platform()
{
    return $this->belongsTo('App\Platform');
}

这篇关于Laravel 5中的3个模型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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