Hi all, im stuck in relationships hell.
My app tracks sports events, and i have what should be a really simple relationship between teams and games. There are many teams, and each game has a home team and an away team, both of which are (obviously) teams.
For the life of me I cannot seem to get this working to my satisfaction, the game database has home_team_id and away_team_id columns.
I want to just implement a hasOne relationship between the game model to a team model through the hometeam label and the same for the awayteam. So i can do:
public function GetMatch($matchId)
{
return Match::with(
array(
'homeTeam',
'awayTeam',
)->find($matchId);
}
But for hasone relationships, laravel expects my teams to have a match_id attribute, and that doesnt make sense to me here, the game model specifies which teams it has and which is the home team etc. the teams can belong to many games.
Help?
I understand your problem.
That is a One-to-One relationship.
BUT, using hasOne is wrong. It would be right if you hadn't any foreign keys in your match table, but istead in your team table (match_id). You have to use belognsTo instead. This means that the foreign key is in the table of the current model.
The relationship is correct, but you are looking from the wrong side.
How are your relationships defined in your model? Could you post that here?
From my understanding, your Team model should have a "belongsTo" relation towards Match, as you don't want to have a match_id column in your Team.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community