连锁 Laravel traits/scopes
Chain Laravel traits/scopes
我想链接 2 个可通过我创建的特征访问的全局范围。
trait ActivatedTrait
{
public static function bootActivatedTrait()
{
static::addGlobalScope(new ActivatedScope);
}
public static function withInactive()
{
$instance = new static;
return $instance->newQueryWithoutScope(new ActivatedScope);
}
}
trait PublishedTrait
{
public static function bootPublishedTrait()
{
static::addGlobalScope(new PublishedScope);
}
public static function withUnpublished()
{
$instance = new static;
return $instance->newQueryWithoutScope(new PublishedScope);
}
}
当我这样调用我的模型时,它起作用了
MyModel::withInactive()
MyModel::withUnpublished()
但这不是
MyModel::withInactive()->withUnpublished()
编辑
出于某种原因,此代码在 Laravel 4.2 下工作,但我切换到 5.5,现在它崩溃了。
编辑 2
如果我制作像 scopeWithInactive()
和 scopeWithUnpublished()
这样的本地作用域,我可以很好地链接它们。
因为我是这个项目的新手,所以我不太明白正在做什么,因为在升级后那个部分坏了之后我没有必要的洞察力。我所做的是:
消除特征,添加正常的 L 5.5 全局范围(这个范围在每个请求中只获取活动项)
class ActivatedScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('content.is_active', 1);
}
}
在模型中启动它们
protected static function boot()
{
parent::boot();
static::addGlobalScope(new ActivatedScope());
static::addGlobalScope(new PublishedScope());
}
并添加了会取消其效果的本地作用域:
public function scopeWithInactive($query)
{
return $query->withoutGlobalScope(ActivatedScope::class);
}
这使我能够做到这一点:
Item::all() // <- only active and published items
Item::withInactive()->get() // <- published items which are either active or inactive
Item.:withInactive()->withUnpublished()->get() // <- all items from DB
注意
我最初的问题是错误的,因为 "chaining" 这里没有任何意义,因为全局范围会自动应用于模型。如果我使用 2 个全局范围,则两者都会应用。所以这是一个链接函数的问题,它会禁用全局范围的影响。
我想链接 2 个可通过我创建的特征访问的全局范围。
trait ActivatedTrait
{
public static function bootActivatedTrait()
{
static::addGlobalScope(new ActivatedScope);
}
public static function withInactive()
{
$instance = new static;
return $instance->newQueryWithoutScope(new ActivatedScope);
}
}
trait PublishedTrait
{
public static function bootPublishedTrait()
{
static::addGlobalScope(new PublishedScope);
}
public static function withUnpublished()
{
$instance = new static;
return $instance->newQueryWithoutScope(new PublishedScope);
}
}
当我这样调用我的模型时,它起作用了
MyModel::withInactive()
MyModel::withUnpublished()
但这不是
MyModel::withInactive()->withUnpublished()
编辑
出于某种原因,此代码在 Laravel 4.2 下工作,但我切换到 5.5,现在它崩溃了。
编辑 2
如果我制作像 scopeWithInactive()
和 scopeWithUnpublished()
这样的本地作用域,我可以很好地链接它们。
因为我是这个项目的新手,所以我不太明白正在做什么,因为在升级后那个部分坏了之后我没有必要的洞察力。我所做的是:
消除特征,添加正常的 L 5.5 全局范围(这个范围在每个请求中只获取活动项)
class ActivatedScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder->where('content.is_active', 1);
}
}
在模型中启动它们
protected static function boot()
{
parent::boot();
static::addGlobalScope(new ActivatedScope());
static::addGlobalScope(new PublishedScope());
}
并添加了会取消其效果的本地作用域:
public function scopeWithInactive($query)
{
return $query->withoutGlobalScope(ActivatedScope::class);
}
这使我能够做到这一点:
Item::all() // <- only active and published items
Item::withInactive()->get() // <- published items which are either active or inactive
Item.:withInactive()->withUnpublished()->get() // <- all items from DB
注意
我最初的问题是错误的,因为 "chaining" 这里没有任何意义,因为全局范围会自动应用于模型。如果我使用 2 个全局范围,则两者都会应用。所以这是一个链接函数的问题,它会禁用全局范围的影响。