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 –
我的不好,請看更新的答案 –