概念问题:
在使用touches
属性时,我遇到了一个非常简单的问题,它可以自动更新依赖模型上的时间戳;它确实能够正确地执行此操作,但也应用了全局范围。
有没有办法关闭此功能?或者要求特别的自动touches
忽略全局范围?
具体例子:
当更新配料模型时,所有相关的食谱都应该被触发。这个功能很好,但是我们有一个globalScope
来根据语言环境分离食谱,在应用touches时也会使用它。
配料模型:
class Ingredient extends Model
{
protected $touches = ['recipes'];
public function recipes() {
return $this->belongsToMany(Recipe::class);
}
}
食谱模型:
class Recipe extends Model
{
protected static function boot()
{
parent::boot();
static::addGlobalScope(new LocaleScope);
}
public function ingredients()
{
return $this->hasMany(Ingredient::class);
}
}
本地化范围:
class LocaleScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$locale = app(Locale::class);
return $builder->where('locale', '=', $locale->getLocale());
}
}
touches
属性传递参数?没有这个关键信息,这不是一个解决方案。 - Nicklas Kevin Frank