laravel - Eloquent relation with two keys -
i working on team sports statistics project in laravel.
here sketch of main models regarding question:
class team // belongstomany matches // belongstomany people class match // belongstomany teams // belongstomany people class person // belongstomany matches // belongstomany teams
i have made models pivot tables work directly them. have stuff goals_scored
, number
, position
etc. stored there , it's clumsy use methods updating pivot.
class matchteam // participation of team in match // belongsto match // belongsto team class matchperson // participation of person in match // belongsto match // belongsto team // belongsto person
is there eloquent way relate matchteam
matchperson
? don't want introduce direct match_team_id
foreign key, want using existing match_id
, team_id
fields on both of tables.
currently have in matchteam model:
public function matchpeople() // roster { return $this->hasmany('app\stats\matchperson', 'team_id', 'team_id')->where('match_id', $this->match_id); }
it works fine. however, concerned code not fair nature of relation - not related based on team_id
, related based on both fields. have written this:
public function matchpeople() // roster { return $this->hasmany('app\stats\matchperson', 'match_id', 'match_id')->where('team_id', $this->team_id); }
is there fair way in specify relation based on 2 columns in eloquent?
of course, question more out of curiosity actual problem of making method conquered.
a usage example curious:
$teama->goals_for = $teama->matchpeople->sum('goals');
Comments
Post a Comment