Laravel 5.7。我有一个 Eloquent 模型Theme,有两个字段,id和name.另一个模型 ,Filter具有三个字段:id, name, theme_id。筛选:class Filter extends Model{ public function theme() { return $this->belongsTo('App\Theme'); }}我可以将这些过滤器附加到任意数量的其他模型上,例如该模型Foo有两个字段,id并且name. 我使用Filterables表格将过滤器附加到它:过滤器:id | filter_id | filterable_id | filterable_type------------------------------------------------1 | 1 | 3 | App\Foo上面的例子中示出了Filter的id1被附接到Foo的id3。现在要获取 的所有过滤器Foo,我的Foo模型中有以下关系:福:class Foo extends Model{ public function filters() { return $this->morphToMany('App\Filter', 'filterable'); }}这一切正常。但是现在我想获取Foo模型,并且只有过滤器所在的过滤器theme_id(例如)1。如何将此条件添加到filters关系中?
1 回答
繁花如伊
TA贡献2012条经验 获得超12个赞
只需将 where 子句直接添加到您的关系中
class Foo extends Model
{
public function filters()
{
return $this->morphToMany('App\Filter', 'filterable')->where('theme_id', 1);
}
}
- 1 回答
- 0 关注
- 155 浏览
添加回答
举报
0/150
提交
取消