使用Laravel5.6 hasManyThrough

时间:2019-01-26 22:35:36

标签: laravel-5.6

我正在建立一个包含团队,团队比赛和比赛活动的网站。每个小组赛都有一个主队和一个客队。每个比赛项目都有一个团队比赛和一个团队。

问题是我得到了特定团队比赛的团队比赛事件,但是对于主队我也没有团队比赛。

团队

id |名称

team_matches

id | home_team_id | away_team_id

team_match_events

id | team_match_id | team_id |事件

团队模型

public function homeTeamMatch()
{
    return $this->hasMany(TeamMatch::class, 'home_team_id');
}

public function awayTeamMatch()
{
    return $this->hasMany(TeamMatch::class, 'away_team_id');
}

TeamMatch模型

public function homeTeam()
{
    return $this->belongsTo(Team::class, 'home_team_id');
}

public function awayTeam()
{
    return $this->belongsTo(Team::class, 'away_team_id');
}

public function homeTeamMatchEvent()
{
    return $this->hasManyThrough(
        TeamMatchEvent::class, 
        TeamMatch::class,
        'home_team_id', 
        'team_match_id', 
        'id', 
        'id'

    );
}

TeamMatchEvent模型

public function teamMatch()
{
    return $this->belongsTo(TeamMatch::class);
}

public function team()
{
    return $this->belongsTo(Team::class);
}

在HomeController中

    $last_match_results = TeamMatch::with('homeTeam.homeTeamMatchEvent')->first();

当我获取球队比赛时,我想获取主队以及该特定球队比赛的相关比赛事件,而客队则相同。

0 个答案:

没有答案
相关问题