在laravel模型中的scope范围查询之查询作用域
Laravel  /  管理员 发布于 2年前   701
本文简单了解一下laravel Scope的全局范围、本地范围、动态作用域等使用示例代码
全局范围
让我们在app/Scopes/HasActiveScope.php文件中创建一个新的类, 内容如下
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class HasActiveScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->where('active', true);
}
}
你应该覆盖模型的booted方法并调用模型的addGlobalScope方法。
addGlobalScope 方法接受你的作用域的一个实例作为它的唯一参数
<?php
namespace App\Models;
use App\Scopes\HasActiveScope;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
// using seperate scope class
static::addGlobalScope(new HasActiveScope);
// you can do the same thing using anonymous function
// let's add another scope using anonymous function
static::addGlobalScope('delete', function (Builder $builder) {
$builder->where('deleted', false);
});
}
使用全局范围
# fetching all active and non-deleted users
$users = User::all();
# above query will result in following sql
select * from `users` where `active` = 1 and `deleted` = 0
从查询中删除范围
你可能不希望你的全局过滤器被应用。例如,如果你想获取所有的用户,无论他们是否处于活动状态。
在这种情况下,你可以用下面的例子删除你应用的范围
# fetch all users weather they are active or not
$users = User::withoutGlobalScope(new HasActiveScope)->all();
# fetch active users weather they are deleted or not
User::withoutGlobalScope('delete')->all();
本地范围
本地作用域是以Scope关键字为前缀的。在这种情况下,你可以在你的Laravel模型中添加本地作用域,而不是定义全局作用域,如下所示
<?php
namespace App\Models;
use App\Scopes\HasActiveScope;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function scopeActive($query)
{
return $query->where('active', 1);
}
public function scopeDelete($query)
{
return $query->where('delete', 0);
}
}
使用本地范围
# apply active local scope to following query
$users = User::active()->orderBy('created_at')->get();
# apply delete local scope to following query
$users = User::delete()->orderBy('created_at')->get();
动态作用域
有时你可能想给你的局部作用域传递额外的参数。在这种情况下,我们可以向我们的局部作用域传递动态参数,它将作为动态作用域。
<?php
namespace App\Models;
use App\Scopes\HasActiveScope;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function scopeActive($query)
{
return $query->where('active', 1);
}
public function scopeDelete($query)
{
return $query->where('delete', 0);
}
public function scopeWithRole($query, $role)
{
return $query->where('role', $role);
}
}
使用动态作用域
# find active users with role admin
$users = User::active()->withRole('admin')->all();
我希望对你有所帮助!
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号