简体   繁体   English

HasManyThrough与Laravel 5.2中的另一个关系

[英]HasManyThrough with another relationship in Laravel 5.2

I have 3 models: 我有3种型号:

Tournament, Category, Team 比赛,类别,团队

A Tournament hasMany Category 比赛有很多类别

A Category hasMany Team 一个类别有很多团队

Tables: 表格:

Tournament: Only attributes
Category: id, tournament_id, name
Teams: id, category_id, name

I would like to get all teams from a tournament by: $tournament->teams 我想通过以下方式让所有队伍参加比赛:$ tournament-> teams

I tried : 我试过了 :

public function teams()
{
    return $this->hasManyThrough(Team::class,CategoryTournament::class);
}

Then I need an extra relation of my team: team->category->name; 然后,我需要团队之间的额外联系:team-> category-> name;

but the result of this HasManyThrough has no relationship.... 但是此HasManyThrough的结果没有任何关系。

Any Idea??? 任何想法???

In Category model : Category模型中:

public function team ()
{
  return $this->hasMany('App\Teams');
}

In Teams model : Teams模型中:

public function category ()
{
   return $this->belongsTo('App\Category', 'category_id');
}

I think should be like this, Try. 我认为应该是这样,尝试。

According to laravel documentation : 根据laravel文档:

countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string

With this you can add relationship : 有了这个你可以添加关系:

public function posts()
    {
        return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM