Laravel Eloquent Relationship Through Tables

2019-09-05 00:05发布

问题:

I have the following database tables:

users

  • id
  • name

seasons

  • id
  • name

teams

  • id
  • name

standings

  • id
  • season_d
  • team_id
  • user_id

fixtures

  • id
  • season_id
  • home_team_id
  • away_team_id

My question is, how would I get which user a team belongs to for a certain fixture? For example I may want to do the following:

$fixture = Fixture::find(1);

echo $fixture->homeTeam->user->name;

My models look like this:

Fixture Model

class Fixture extends Eloquent{

    public function season(){

        return $this->belongsTo('Season');

    }

    public function homeTeam(){

        return $this->belongsTo('Team', 'home_team_id');

    }

    public function awayTeam(){

        return $this->belongsTo('Team', 'away_team_id');

    }

}

Team Model

class Team extends Eloquent{

    public function standings(){

        return $this->hasMany('Standing');

    }

    public function seasons(){

        return $this->belongsToMany('Season', 'Standings');

    }

    public function users(){

        return $this->belongsToMany('User', 'Standings');

    }

}

Season Model

class Season extends Eloquent{

    public function standings(){

        return $this->hasMany('Standing');

    }

    public function teams(){

        return $this->belongsToMany('Team', 'Standings');

    }

    public function users(){

        return $this->belongsToMany('User', 'Standings');

    }

    public function fixtures(){

        return $this->hasMany('Fixture');

    }

}

I think I need to add a user function to the Team model instead of the current users function that's there, but I can't figure out the correct way to do the relationship. A team will only have one user for any given season. Any help would be appreciated, thanks!

UPDATE

I have added the following relationships to the Fixture model, which allows me to get the user and team through the standings table:

public function homeTeamStanding(){

    return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'home_team_id', 'team_id')->where('season_id', $this->season_id);

}

public function awayTeamStanding(){

    return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'away_team_id', 'team_id')->where('season_id', $this->season_id);

}

The problem with this is that I can't use it with eager loading, so there's quite a lot of queries running, as when I try to eager load them $this->season_id is null. Surely there's a better way?

回答1:

I replaced the above with joins instead so that I can eager load which results in a lot less queries!

public function homeTeamStanding(){

    return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'home_team_id', 'team_id')
        ->join('fixtures', 'fixtures.season_id', '=', 'standings.season_id');

}

public function awayTeamStanding(){

    return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'away_team_id', 'team_id')
        ->join('fixtures', 'fixtures.season_id', '=', 'standings.season_id');

}