Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.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
Php 加入多对多关系-Laravel_Php_Laravel - Fatal编程技术网

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