简体   繁体   English

Laravel / Eloquent关系中的多个连接规则

[英]Multiple join rules in Laravel/Eloquent relation

So, I'm developing a sports system where games table has 2 relations for the same table teams . 所以,我正在开发一个体育系统,其中games桌有两个关系用于同一桌面teams I know the context usuallty doesn't matter, but I'm trying to make it clear why the database is stuctured that way. 我知道上下文通常并不重要,但我试图弄清楚为什么数据库被这样结构化了。 The relations are stored as games.home_id references teams.id and games.away_id references teams.id to link the 2 teams in the same game. 关系存储为games.home_id references teams.idgames.away_id references teams.id来链接同一游戏中的2个团队。 The tables structures are 表结构是

- games
  - id
  - home_id
  - away_id
  - starts_at

- teams
  - id

- team_players
  - id
  - team_id

So, if I want to get all the players of the games to be played today I'll do 所以,如果我想让今天所有的比赛球员参加比赛,我会做的

SELECT team_players.*
FROM team_players
    JOIN teams ON (teams.team_id = team_players.team_id)
    JOIN games ON (teams.id = games.home_id OR teams.id = games.away_id)
WHERE games.starts_at <= $starts AND  games.starts_at >= $ends

How do I create the hasMany relationships in the models to include both (team.id = games.home_id OR team.id = games.away_id) ? 如何在模型中创建hasMany关系以包含两者(team.id = games.home_id OR team.id = games.away_id)

I already tried something like 我已经尝试过类似的东西

class Team { 
    public function game() 
    {
        return $this->hasMany(Game::class); 
    }
}

class Game {
    public function teams()
    {
        $rel = $this->hasMany(Game::class, 'home_id'); 
        $rel->orHasMany(Game::class, 'home_id');
        return $rel;
    }
}

but there is no orHasMany() ; 但是没有orHasMany() ;

Thank you. 谢谢。

I think you can define two has-many-through relationships and merge them, it is not tested, but I think it can be the solution for you, please test it and let me know, also you may need to change a little bit about the keys, I have just written to give you some idea 我认为你可以定义两个has-many-through关系并合并它们,它没有经过测试,但我认为它可以是你的解决方案,请测试它并让我知道,你也可能需要改变一下钥匙,我刚写的给你一些想法

class Game extends Model
{
    public function homeTeamPlayers()
    {
        return $this->hasManyThrough(
            'App\TeamPlayer',
            'App\Team',
            'id', // Foreign key on teams table...
            'team_id', // Foreign key on team_players table...
            'home_id', // Local key on games table...
            'id' // Local key on teams table...
        );
    }

     public function awayTeamPlayers()
     {
        return $this->hasManyThrough(
            'App\TeamPlayer',
            'App\Team',
            'id', // Foreign key on teams table...
            'team_id', // Foreign key on team_players table...
            'away_id', // Local key on games table...
            'id' // Local key on teams table...
        );
    }

    public function teamPlayers()
    {
        $this->homeTeamPlayers->merge($this->awayTeamPlayers);
    }
}

now, for every game you can retrieve like 现在,对于你可以检索的每一款游戏

$game = Game::find(1);
$game->teamPlayers();

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

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