Laravel雄辩的关系通过表 [英] Laravel Eloquent Relationship Through Tables
问题描述
我有以下数据库表:
用户
- id
- 名称
季节
- id
- 名称
$ b
- id
- 名称
排名
- id
- season_d
- team_id
- user_id
fixtures
- id
- season_id
- home_team_id
- away_team_id
我的问题是,如何获得一个团队属于某个夹具的用户?例如,我可能想要执行以下操作:
$ fixture = Fixture :: find(1);
echo $ fixture-> homeTeam-> user-> name;
我的模型如下所示:
装置模型
class Fixture扩展Eloquent {
public function season ){
return $ this-> belongsTo('Season');
}
public function homeTeam(){
return $ this-> belongsTo('Team','home_team_id');
}
public function awayTeam(){
return $ this-> belongsTo('Team','away_team_id');
}
}
团队模型
class Team extends Eloquent {
public function standings(){
return $ this-> hasMany('Standing');
}
public function seasons(){
return $ this-> belongsToMany('Season','Standings');
}
public function users(){
return $ this-> belongsToMany('User','Standings');
}
}
季节模型
class季节延伸Eloquent {
public function standings(){
return $ this-> hasMany('Standing');
}
public function teams(){
return $ this-> belongsToMany('Team','Standings');
}
public function users(){
return $ this-> belongsToMany('User','Standings');
}
public function fixtures(){
return $ this-> hasMany('Fixture');
}
}
我想我需要在Team模式中添加一个用户函数,而不是当前用户的功能,但是我无法找出正确的关系方式。任何一个季节,一个团队只有一个用户。谢谢!谢谢!
更新
我已经添加以下与Fixture模型的关系,允许我通过统计表获取用户和团队:
public function homeTeamStanding() {
return $ this-> belongsTo('App \Modules\Leagues\Models\Standing','home_team_id','team_id') - > where('season_id' $这 - > season_id);
}
public function awayTeamStanding(){
return $ this-> belongsTo('App \Modules\Leagues\Models \Standing','away_team_id','team_id') - > where('season_id',$ this-> season_id);
}
这个问题是我无法使用它随着加载,所以有很多查询运行,当我试图加载他们的$ this-> season_id是null。确实有一个更好的方法?
我用联接替代了上面的代码,以便我能够加载更多,导致更少的查询!
public function homeTeamStanding(){
return $ this-> belongsTo('App \Modules\Leagues\Models\Standing','home_team_id','team_id')
- > join('fixtures','fixtures.season_id','=','standings.season_id' );
}
public function awayTeamStanding(){
return $ this-> belongsTo('App \Modules\Leagues\Models \Standing','away_team_id','team_id')
- > join('fixtures','fixtures.season_id','=','standings.season_id');
}
I have the following database tables:
users
- id
- name
seasons
- id
- name
teams
- id
- name
standings
- id
- season_d
- team_id
- user_id
fixtures
- id
- season_id
- home_team_id
- away_team_id
My question is, how would I get which user a team belongs to for a certain fixture? For example I may want to do the following:
$fixture = Fixture::find(1);
echo $fixture->homeTeam->user->name;
My models look like this:
Fixture Model
class Fixture extends Eloquent{
public function season(){
return $this->belongsTo('Season');
}
public function homeTeam(){
return $this->belongsTo('Team', 'home_team_id');
}
public function awayTeam(){
return $this->belongsTo('Team', 'away_team_id');
}
}
Team Model
class Team extends Eloquent{
public function standings(){
return $this->hasMany('Standing');
}
public function seasons(){
return $this->belongsToMany('Season', 'Standings');
}
public function users(){
return $this->belongsToMany('User', 'Standings');
}
}
Season Model
class Season extends Eloquent{
public function standings(){
return $this->hasMany('Standing');
}
public function teams(){
return $this->belongsToMany('Team', 'Standings');
}
public function users(){
return $this->belongsToMany('User', 'Standings');
}
public function fixtures(){
return $this->hasMany('Fixture');
}
}
I think I need to add a user function to the Team model instead of the current users function that's there, but I can't figure out the correct way to do the relationship. A team will only have one user for any given season. Any help would be appreciated, thanks!
UPDATE
I have added the following relationships to the Fixture model, which allows me to get the user and team through the standings table:
public function homeTeamStanding(){
return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'home_team_id', 'team_id')->where('season_id', $this->season_id);
}
public function awayTeamStanding(){
return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'away_team_id', 'team_id')->where('season_id', $this->season_id);
}
The problem with this is that I can't use it with eager loading, so there's quite a lot of queries running, as when I try to eager load them $this->season_id is null. Surely there's a better way?
I replaced the above with joins instead so that I can eager load which results in a lot less queries!
public function homeTeamStanding(){
return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'home_team_id', 'team_id')
->join('fixtures', 'fixtures.season_id', '=', 'standings.season_id');
}
public function awayTeamStanding(){
return $this->belongsTo('App\Modules\Leagues\Models\Standing', 'away_team_id', 'team_id')
->join('fixtures', 'fixtures.season_id', '=', 'standings.season_id');
}
这篇关于Laravel雄辩的关系通过表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!