2017-02-21 35 views
1

的belongsToMany其他表的列這是我對病人的型號代碼:PHP/laravel - 選擇使用laravel

class Patient extends Model 
{ 
    protected $primaryKey = 'PatientID'; 

    public function users() 
    { 
     return $this->belongsToMany('App\Vaccine', 'immunizations', 'patient_id', 'vaccine_id'); 
    } 
}  

,這是我的查詢

$patients = Patient::whereDoesntHave('users', function ($q) use ($vaccine_id) { 
    $q->where('vaccine_id', '=', $vaccine_id); 
})->get(); 

在我目前的狀況,我只可以獲取患者模型列數據,但不是其他表格。我應該如何選擇select()方法來選擇免疫接種表的行和列,因爲我想設置一個鏈where()或條件是where('immunizations_id', 1),但由於未選擇免疫接種表,因此它不起作用。

或是否有人在這裏知道如何將它轉換,而無需使用封閉,比如讓用戶

Patient::wheredoesnthave(join('table'));,所以我可以隨意操縱它

+0

您是否建立了免疫關係? –

+0

我認爲$ this-> belongsToMany設置關係 – Christian

回答

0

我們會得到使用「有()」

其他關係表
$patients = Patient::whereDoesntHave('users', function ($q) use ($vaccine_id) { 
    $q->where('vaccine_id', '=', $vaccine_id); 
    $q->with('Vaccine', 'immunizations'); 
})->get(); 

試試這個。

+0

如果我想數它們呢? – Christian

+0

爲了獲得計數,我們可以使用count()而不是get()。 –