Php 加入多对多关系-Laravel
我在多对多关系中有两个表Php 加入多对多关系-Laravel,php,laravel,Php,Laravel,我在多对多关系中有两个表国家和团队表。在我的国家表中,有总统id。现在,我想使用连接查询获得属于该主席id的团队,但我无法实现这一点 我如何选择属于某一特定总裁的所有团队 PS:拉威尔的新手 国家 public function teams() { return $this->belongsToMany('App\Team'); ->withTimestamps(); } 团队 public function countries()
国家和团队表
。在我的国家表中,有总统id
。现在,我想使用连接查询获得属于该主席id的团队,但我无法实现这一点
我如何选择属于某一特定总裁的所有团队
PS:拉威尔的新手
国家
public function teams()
{
return $this->belongsToMany('App\Team');
->withTimestamps();
}
团队
public function countries()
{
return $this->belongsToMany('App\Country')
->withTimestamps();
}
控制器
$teams = Team::all()->with('countries')->where('president_id,1)->first();
$getTeams = $teams->pluck('id')->toArray();
您可以获得所有具有一些
总统id的国家/地区的团队,如下所示:
$president_id = 1;
$teams = Team::whereHas('countries',function($q)use($president_id){
$q->where('president_id',$president_id);
})->get();
您可以获得所有具有一些总统id的国家/地区的团队,如下所示:
$president_id = 1;
$teams = Team::whereHas('countries',function($q)use($president_id){
$q->where('president_id',$president_id);
})->get();