簡體   English   中英

加入多對多關系-Laravel

[英]JOINS with Many to Many Relationship - Laravel

我在多對多關系countries and teams table有兩個countries and teams table 在我的國家/地區表中,有president_id 現在,我想使用聯接查詢獲取屬於該President_id的團隊,但我無法實現這一目標。

如何才能選擇屬於特定總裁的所有團隊?

PS :新手與laravel

國家

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

您可以使所有國家/地區的國家/地區都帶有president_id如下所示:

$president_id = 1;
$teams = Team::whereHas('countries',function($q)use($president_id){
             $q->where('president_id',$president_id);
         })->get();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM