mil*_*526 1 orm laravel eloquent
我正在尝试为我的 Laravel 网络应用程序制作一个更详细的注册表单。
我有一个Category,Question和Survey模型。
一个类别hasMany Question的和一个Question belongsToa Survey。
类别类:
public function questions()
{
return $this->hasMany('App\Question');
}
public function getUsingAttribute()
{
return $this->questions->contains('survey_id', Survey::current()->id);
}
Run Code Online (Sandbox Code Playgroud)
我目前using在我的 Category 类中有一个属性,但我想把它作为一个范围。
需要明确的是,预期收益Category::using->get()将返回所有Category在那里的问题,至少有一个survey_id的Survey::current()
我会使用可用于查询关系存在的方法之一来完成它,特别是该whereHas()方法:
return $this->whereHas('questions', function ($query){
$query->where('survey_id', Survey::current());
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3156 次 |
| 最近记录: |