Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 两把钥匙的雄辩关系_Laravel_Eloquent - Fatal编程技术网

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');