简体   繁体   English

Laravel:团队与比赛之间的关系

[英]Laravel: relationship between Teams and Matches

I'm currently making a small website where volley-ball Teams will play some Matches . 目前,我正在做一个小网站,凌空球Teams会打一些Matches Here is what I have for now: 这是我现在拥有的:

Match model: Match模型:

class Match extends Model
{
    public function Team1() {
        return $this->belongsTo('App\Team', 'team_1');
    }

    public function Team2() {
        return $this->belongsTo('App\Team', 'team_2');
    }
}

For example, $match->team1->name works well. 例如, $match->team1->name效果很好。

Team model: Team模式:

class Team extends Model
{
    public $timestamps = false;

    public function Matches() {
        return $this->hasMany('App\Match');
    }
}

But $team->matches gives this error: 但是$team->matches会出现此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'matches.team_id' in 'where clause'

Which is normal because Laravel tries to find a team_id field in database but there's actually two: team_1 and team_2 . 这是正常的,因为Laravel试图在数据库中找到一个team_id字段,但实际上有两个: team_1team_2

I'm thinking that relationships between my models might be wrong but I'm not sure. 我认为我的模型之间的关系可能是错误的,但我不确定。

Thanks for your help ! 谢谢你的帮助 !

You are using the right relationship for the data structure you have, but there are two possible ways a team can have a match. 您正在为数据结构使用正确的关系,但是团队可以通过两种方式进行比赛。

class Team extends Model
{
    public function matchesAsTeam1()
    {
        return $this->hasMany('App\Match', 'team_1');
    }

    public function matchesAsTeam2()
    {
        return $this->hasMany('App\Match', 'team_2');
    }
}

To get all matches in a single relation is slightly tricky, and requires knowing about the ->union() method. 要获得单个关系中的所有匹配项会有些棘手,并且需要了解->union()方法。

public function matches()
{
    return $this->matchesAsTeam1()->union($this->matchesAsTeam2());
}

This will get all matches where the team is either team 1 or team 2 of the match. 这将获得所有队伍为队伍1或队伍2的比赛。

Unfortunately, this doesn't work with eager loading. 不幸的是,这不适用于急切的加载。 For example 例如

App\Team::with('matches')->get();

will work to get the matches where that team is team_1 but not the matches where that team is team_2 . 将努力取得比赛在那里的那支球队是team_1但并非如该球队的比赛team_2 If you find yourself using these relations in the context of ->with('matches') , ->has('matches') , and ->whereHas('matches', function($subquery) {}) , this approach will prove problematic in my experience. 如果您发现自己在->with('matches')->has('matches')->whereHas('matches', function($subquery) {})的上下文中使用这些关系,则此方法将我的经验证明存在问题。

I might consider using a many-to-many relationship here, despite the fact that "many" in the second case is always 2. That would involve defining a match_team table: 我可能考虑在这里使用多对多关系,尽管第二种情况下的“许多”始终为2。这将涉及定义match_team表:

Schema::create('match_team', function($table) 
{
    $table->increments('id');
    $table->integer('team_id')->unsigned();
    $table->foreign('team_id')->references('id')->on('teams');
    $table->integer('match_id')->unsigned();
    $table->foreign('match_id')->references('id')->on('matches');
});

Then your relations end up being 然后你的关系最终被

class Team extends Model
{
    public function matches()
    {
        return $this->belongsToMany(Match::class);
    }
}

class Match extends Model
{
    public function teams()
    {
        return $this->belongsToMany(Team::class);
    }
}

If you do use that approach, you'll need to ensure that you enforce that each match has exactly 2 teams. 如果您确实使用这种方法,则需要确保您强制每场比赛有2支球队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM