2017-04-08 2 views
1

私はLaravel Webアプリケーションのためのより精巧な登録フォームを作成しようとしています。
私はCategory,QuestionおよびSurveyモデルを持っています。
カテゴリhasManyQuestionおよびQuestionbelongsToSurveyです。Laravelは、関連性があるモデルのコレクションを取得します

Categoryクラス:

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

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

私は現在、私のCategoryクラスでusing属性を持っていますが、私はこのスコープしたいと思います。明確にするため
Category::using->get()の期待収益が質問の少なくとも一方が

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

答えて

1

について、私はのいずれかの方法でそれを行うだろう何Survey::current()

+1

私はスコープでこれを入れて、 '$ QUに' $ this'を変更これは完璧に機能しました – milo526

0

survey_idを有する場合、すべてのCategoryを返しますquery a relationship existenceに利用できる、特にwhereHas()方法:

return $this->whereHas('questions', function ($query){ 
    $query->where('survey_id', Survey::current()); 
}); 
関連する問題