本地作用域:
在模型Model中定义
public function scopeMe($query)
{
$query->where('owner_id','=',Auth::guard('api')->id());
}
在控制器的使用
$list = Template::where('is_check', $request->type)->me()->orderBy('created_at', 'desc')->paginate(10);
全局作用域
全局范围能为给定模型的所有查询添加约束。Laravel 自带的 软删除功能 就利用全局作用域从数据库中提取「未删除」的模型。编写自定义的全局作用域可以提供一个方便、简单的方法来确保给定模型的每个查询都受到一定的约束。
编写全局作用域
编写全局作用域很简单。首先定义一个实现 Illuminate\Database\Eloquent\Scope
接口的类。这个接口要求你实现一个方法:apply
。apply
方法可以根据需要添加 where
条件到查询:
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class AgeScope implements Scope
{
/**
* 将范围应用于给定的 Eloquent 查询生成器
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
return $builder->where('age', '>', 200);
}
}
{tip} 如果全局作用域要将字段添加到查询的 select 语句中,则应该使用
addSelect
方法而不是select
。这是用来防止可能会无意中替换了查询的现有 select 语句。
应用全局作用域
要将全局作用域分配给模型,需要重写给定模型的 boot
方法并使用 addGlobalScope
方法:
<?php
namespace App;
use App\Scopes\AgeScope;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* 模型的「启动」方法
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new AgeScope);
}
}
添加作用域后,如果使用 User::all()
查询则会生成如下 SQL 语句:
select * from `users` where `age` > 200
匿名的全局作用域
Eloquent 还能使用闭包定义全局作用域,如此一来,便就没必要定义一个单独的类了:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class User extends Model
{
/**
* 模型的「启动」方法
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope('age', function(Builder $builder) {
$builder->where('age', '>', 200);
});
}
}
删除全局作用域
如果要删除给定的查询的全局作用域,则可以使用 withoutGlobalScope
方法。该方法接受全局作用域的类名作为其唯一参数:
User::withoutGlobalScope(AgeScope::class)->get();
如果你想要删除几个甚至全部的全局作用域,可以使用 withoutGlobalScopes
方法:
// 删除所有的全局作用域
User::withoutGlobalScopes()->get();
// 删除一些全局作用域
User::withoutGlobalScopes([
FirstScope::class, SecondScope::class
])->get();