2017-04-08 36 views
1

我想爲我的Laravel Web應用程序製作更詳細的註冊表單。
我有一個Category,QuestionSurvey模型。
A category hasManyQuestion's and a QuestionbelongsTo a SurveyLaravel獲得關係具有價值的模型集合

分類等級:

public function questions() 
{ 
    return $this->hasMany('App\Question'); 
} 

public function getUsingAttribute() 
{ 
    return $this->questions->contains('survey_id', Survey::current()->id); 
} 

我目前確實有using屬性在我的範疇類,但我想使這個範圍。
要清晰,Category::using->get()預期收益將返回所有Category其中的問題至少有survey_idSurvey::current()

回答

1

我會用的方法之一做可供query a relationship existence,特別是whereHas()方法:

return $this->whereHas('questions', function ($query){ 
    $query->where('survey_id', Survey::current()); 
}); 
+1

我把這個在一個範圍內,改變了'$ this'成'$曲這個工作完美 – milo526

0

什麼

return $this->questions->where('survey_id', Survey::current()->id);