在没有全局范围的情况下使用 Laravel 次触摸
Use Laravel touches without global scopes
概念问题:
使用 touches
属性时,我遇到一个非常简单的问题,即自动更新依赖模型的时间戳;它正确地这样做了,但也适用于全球范围。
有什么办法可以关闭这个功能吗?或者专门要求 automatic 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());
}
}
如果您想明确避免给定查询的全局范围,您可以使用 withoutGlobalScope()
方法。该方法接受全局范围的 class 名称作为其唯一参数。
$ingredient->withoutGlobalScope(LocaleScope::class)->touch();
$ingredient->withoutGlobalScopes()->touch();
由于您没有直接调用 touch()
,因此在您的情况下需要更多时间才能使其正常工作。
您指定应在模型 $touches
属性中触及的关系。关系 return 查询生成器对象。看到我要去的地方了吗?
protected $touches = ['recipes'];
public function recipes() {
return $this->belongsToMany(Recipe::class)->withoutGlobalScopes();
}
如果这与您的应用程序的其余部分混淆,只需创建一个专门用于触摸的新关系 (嘿 :)
protected $touches = ['recipesToTouch'];
public function recipes() {
return $this->belongsToMany(Recipe::class);
}
public function recipesToTouch() {
return $this->recipes()->withoutGlobalScopes();
}
您可以在模型中定义关系并将参数传递给它,如下所示:
public function recipes($isWithScope=true)
{
if($isWithScope)
return $this->belongsToMany(Recipe::class);
else
return $this->recipes()->withoutGlobalScopes();
}
然后像这样使用它 recipes->get();
和 recipes(false)->get();
概念问题:
使用 touches
属性时,我遇到一个非常简单的问题,即自动更新依赖模型的时间戳;它正确地这样做了,但也适用于全球范围。
有什么办法可以关闭这个功能吗?或者专门要求 automatic 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());
}
}
如果您想明确避免给定查询的全局范围,您可以使用 withoutGlobalScope()
方法。该方法接受全局范围的 class 名称作为其唯一参数。
$ingredient->withoutGlobalScope(LocaleScope::class)->touch();
$ingredient->withoutGlobalScopes()->touch();
由于您没有直接调用 touch()
,因此在您的情况下需要更多时间才能使其正常工作。
您指定应在模型 $touches
属性中触及的关系。关系 return 查询生成器对象。看到我要去的地方了吗?
protected $touches = ['recipes'];
public function recipes() {
return $this->belongsToMany(Recipe::class)->withoutGlobalScopes();
}
如果这与您的应用程序的其余部分混淆,只需创建一个专门用于触摸的新关系 (嘿 :)
protected $touches = ['recipesToTouch'];
public function recipes() {
return $this->belongsToMany(Recipe::class);
}
public function recipesToTouch() {
return $this->recipes()->withoutGlobalScopes();
}
您可以在模型中定义关系并将参数传递给它,如下所示:
public function recipes($isWithScope=true)
{
if($isWithScope)
return $this->belongsToMany(Recipe::class);
else
return $this->recipes()->withoutGlobalScopes();
}
然后像这样使用它 recipes->get();
和 recipes(false)->get();