my database has two table games and teams,
games migration:
$table->unsignedBigInteger('homeTeam');
$table->unsignedBigInteger('awayTeam');
$table->foreign('homeTeam')->references('id')->on('teams')
$table->foreign('awayTeam')->references('id')->on('teams')
what is relationship between this two table? many to many? should I create "game_team" pivot table?
i tried to change design of database but it don’t fixed
2
Answers
More details are needed for a more accurate design of the database, but anyway, according to your explanation, the relationship between the games and teams table must be
one-to-many
.actually you need two one-to-many relation . Each game has two foreign keys, one for the home team and another for the away team, which both reference the
id
column in theteams
table. that each game belongs to one home team and one away team, and each team can have multiple games associated with it.what you describe is two 1:n relationships.
A game has one home team and one away team.
You don’t need an additional table.
You should use names with _id suffix for foreign keys because laravel models like it that way.
In your Game model you can then say
to use it like
That is the magic of laravel (eloquent).
Cheers