0
概念问题: 我使用touches
属性时,自动更新上取决于模型的时间戳有一个非常简单的问题;它正确地这样做,但也适用于全球范围。使用Laravel倒是没有全球范围
有什么方法可以关闭此功能吗?或者专门要求自动touches
忽略全局范围?
具体实例: 当配料模型更新所有相关的食谱应该被感动。这工作正常,除了我们有一个globalScope
根据区域设置分开配方,这也适用于触摸时使用。
成分型号:
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());
}
}
如前所述,我们没有显式调用'触摸()'方法,'touch'会自动通过属性'$ touch.'调用Laravel –
我的不好,请看更新的答案 –