您好,我的模型类别具有全局范围:
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope('active', function (Builder $builder) {
$builder->where('active', true);
});
}
public function products()
{
return $this->hasMany(Product::class);
}
模型产品:
protected static function boot()
{
parent::boot();
static::addGlobalScope('active', function (Builder $builder) {
$builder->where('active', true);
});
}
public function category()
{
return $this->belongsTo(Category::class);
}
我需要做,如果类别未处于活动状态,则设置并且该类别中的产品处于非活动状态。我该怎么做?
现在,当类别无效时,产品可以显示在页面上。我可以在模型产品中添加活动类别的全局范围吗?
答案 0 :(得分:0)
尝试一下(产品型号):
protected static function boot()
{
parent::boot();
static::addGlobalScope('active', function (Builder $builder) {
$builder
->where('active', true)
->whereHas('category', function($query){
$query->where('active', true);
});
});
}