How to Register Eloquent Global Scope in Laravel
Global scopes allow you to add constraints to all queries for a given model. Laravel’s own soft delete functionality utilizes global scopes to only retrieve “non-deleted” models from the database.
In this article, we’ll create our own global scope. Let’s get started:
Table of Contents
Create Global Scope
We have to create scope in app\Scopes folder. Let’s make a scope called ActiveScope.php.
<?php
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class ActiveScope 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('is_active', 1);
}
}
Register Scope in Model
In this step, we’ll define ActiveScope in User model.
<?php
namespace App;
use App\Scopes\ActiveScope;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $fillable = [
'name','email','password','is_active',
];
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::addGlobalScope(new ActiveScope);
}
}
If we run select query User::all()
, it will execute the following SQL query:
select * from `users` where `is_active` = 1
Anonymous Global Scope
Eloquent also allows you to define global scopes using closures, which is particularly useful for simple scopes that do not warrant a separate class of their own.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::addGlobalScope('ancient', function (Builder $builder) {
$builder->where('created_at', '<', now()->subDays(20));
});
}
}
Remove Scope From Query
To remove a global scope for a given query, we can use the withoutGlobalScope
method:
User::withoutGlobalScope(AncientScope::class)->get();
And if we want to removed the global scope using closure (anonymous), we need to write code like:
User::withoutGlobalScope('ancient')->get();
To remove all of the global scopes:
User::withoutGlobalScopes()->get();
To remove some of the global scopes:
User::withoutGlobalScopes([
FirstScope::class, SecondScope::class
])->get();
That’s all, artisans. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)