简体   繁体   中英

HasManyThrough with another relationship in Laravel 5.2

I have 3 models:

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

I tried :

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

Then I need an extra relation of my team: team->category->name;

but the result of this HasManyThrough has no relationship....

Any Idea???

In Category model :

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

In Teams model :

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

I think should be like this, Try.

According to laravel documentation :

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');
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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