I'm working with Laravel 4 and trying to setup a table structure for handling the following problem.
- I have 3 tables: Players, Teams & Seasons
- For each season, I will have multiple teams assigned and each team will have multiple players assigned.
- I need to maintain historical data for each season, so I can't just connect the tables directly because changing the base player/teams tables would affect all seasons that way.
I connected the Seasons -> Teams table by using an intermediate table teams_in_season
as follows:
class Season extends \Eloquent
{
public function teams()
{
return $this->belongsToMany('Team', 'teams_in_season');
}
}
That works as expected. The issue comes when I want to setup the player assignment. Naturally, I want to relate the teams to players so my line of thinking is that I need to create an intermediate table off of another intermediate table. Ex:
seasons -> teams_in_season -> players_in_teams -> players
If I went Seasons -> Players, that would work except that I wouldn't be able to eager load it that way.
seasons -> players_in_season -> players
$season->teams->players->get();
Essentially, the way the user enters data is to create a season, assign teams, assign players to teams, and then eventually add scoring. All data entered needs to be maintained and therefore the intermediate tables are necessary.
So, here's my question(s):
- Can I nest/chain intermediate tables like this?
- Is there a better way I can setup what I want to achieve?