在文章模型中,定义全局scope的方式
protected static function boot()
{
parent::boot();
static::addGlobalScope("avaiable",function (Builder $builder)
{
$builder->whereIn('status',[0,1]);
});
}
在所有页面使用文章模型都带上全局scope 非常方便
如果想不用在某种情况不用全局scope
$posts = Post::withoutglobaScope('avaiable')