私はLaravel Webアプリケーションのためのより精巧な登録フォームを作成しようとしています。
私はCategory
,Question
およびSurvey
モデルを持っています。
カテゴリhasMany
Question
およびQuestion
belongsTo
Survey
です。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);
私はスコープでこれを入れて、 '$ QUに' $ this'を変更これは完璧に機能しました – milo526