繁体   English   中英

HasManyThrough与Laravel 5.2中的另一个关系

[英]HasManyThrough with another relationship in Laravel 5.2

我有3种型号:

比赛,类别,团队

比赛有很多类别

一个类别有很多团队

表格:

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

我想通过以下方式让所有队伍参加比赛:$ tournament-> teams

我试过了 :

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

然后,我需要团队之间的额外联系:team-> category-> name;

但是此HasManyThrough的结果没有任何关系。

任何想法???

Category模型中:

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

Teams模型中:

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

我认为应该是这样,尝试。

根据laravel文档:

countries
    id - integer
    name - string

users
    id - integer
    country_id - integer
    name - string

posts
    id - integer
    user_id - integer
    title - string

有了这个你可以添加关系:

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