Laravel 两把钥匙的雄辩关系
我在拉维尔从事一个团队体育统计项目 以下是有关此问题的主要模型的示意图:Laravel 两把钥匙的雄辩关系,laravel,eloquent,Laravel,Eloquent,我在拉维尔从事一个团队体育统计项目 以下是有关此问题的主要模型的示意图: class Team // belongsToMany matches // belongsToMany people class Match // belongsToMany teams // belongsToMany people class Person // belongsToMany matches // belongsToMany teams 我还为透视表
class Team
// belongsToMany matches
// belongsToMany people
class Match
// belongsToMany teams
// belongsToMany people
class Person
// belongsToMany matches
// belongsToMany teams
我还为透视表制作了模型,以便直接使用它们。我有一些东西,比如进球
,数字
,位置
等,都存储在那里,而且使用方法更新pivot太笨拙了
class MatchTeam // Participation of a team in the match
// belongsTo match
// belongsTo team
class MatchPerson // Participation of a person in the match
// belongsTo match
// belongsTo team
// belongsTo person
是否有一种雄辩的方式将MatchTeam
与MatchPerson
联系起来?我不想引入直接的match\u team\u id
外键,我想使用两个表上现有的match\u id
和team\u id
字段来实现这一点
目前我的MatchTeam模型中有:
public function matchPeople() // the roster
{
return $this->hasMany('App\Stats\MatchPerson', 'team_id', 'team_id')->where('match_id', $this->match_id);
}
它很好用。但是,我非常担心该代码对关系的性质不公平-它们不是基于team_id
的关联,而是基于两个字段的关联。我也可以这样写:
public function matchPeople() // the roster
{
return $this->hasMany('App\Stats\MatchPerson', 'match_id', 'match_id')->where('team_id', $this->team_id);
}
有没有一种公平的方法可以让我根据雄辩中的两列来指定关系
当然,这个问题更多的是出于好奇,因为制造这种方法的实际问题已经被克服了
好奇者的用法示例:
$teamA->goals_for = $teamA->matchPeople->sum('goals');