簡體   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