Laravel 使用全局作用域和局部作用域进行查询
全局作用域
是指在模型类中预设过滤器,在模型类的所有查询中都生效。
通过全局作用域类实现
编写一个实现Illuminate\Database\Eloquent\Scope
接口的全局作用域类,这里我们将其命名为 EmailVerifiedAtScope
,并将其放到 app/Scopes
目录下:
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class EmailVerifiedAtScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
return $builder->whereNotNull('email_verified_at');
}
}
将全局作用域类注册到User
模型,这样在User
模型上进行查询的时候才可以应用相应的过滤条件。重写该模型父类的boot
方法
protected static function boot()
{
parent::boot();
static::addGlobalScope(new EmailVerifiedAtScope());
}
通过匿名函数实现
在模型类的boot
方法中通过匿名函数实现全局作用域:
protected static function boot()
{
parent::boot();
static::addGlobalScope('email_verified_at_scope', function (Builder $builder) {
return $builder->whereNoNull('email_verified_at');
});
}
移除全局作用域
User::withoutGlobalScope(EmailVerifiedAtScope::class)->get(); # 指定类
User::withoutGlobalScope('email_verified_at_scope')->get(); # 匿名函数
User::withoutGlobalScopes()->get(); # 移除所有全局作用域
User::withoutGlobalScopes([FirstScope::class, SecondScope::class])->get(); # 移除多个类/匿名函数
局部作用域
局部作用域在模型类中定义,该方法需要以scope
开头。
public function scopePopular(Builder $query)
{
return $query->where('views','>',0)->orderBy('views','desc');
}
使用:只需调用scope
之后的过滤器名称即可。
$post = Post::popular()->get();
动态作用域
动态作用域和局部作用域类似,只不过可以通过额外参数指定查询条件。
public function scopeOfType(Builder $query, $type)
{
return $query->where('type', $type);
}