Simplified Eloquent Global Scopes in Laravel 5.2
This is a series of posts that showcase new features and aha moments in Laravel 5.2
1. Implicit Route Model Binding
2. Simplified Eloquent Global Scopes
3. Append Scheduled Tasks Output
The use of Eloquent Global scopes has been greatly simplified in Laravel 5.2
Several Laravel Developers love to use the simple scopes to avoid duplicating queries everywhere in the code base.
Let’s take a simple look at Local Scopes
Let’s assume we have a Task Model.
Task.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php namespace App; use Illuminate\Database\EloquentModel; class Task extends Model { /** * Scope a query to only include finished tasks * * @return Illuminate\Database\EloquentBuilder */ public function scopeFinish($query) { return $query->where('status', 'finished'); } } |
So in my controllers, I can do:
Task::finish()->get()
, this will actually return all the finished tasks.
That’s a quick refresher on Local Scopes.
Now, Let’s take a close look at Global Scopes.
In Laravel 5.2, It’s simple to create Global Scopes. There are two ways of going about it:
1. Create a separate class.
2. Use a closure inside the Model to define the Global Scope.
Method 1
Create Folder called Scopes in the app
directory. Let’s create a FinishScope file in it like so:
FinishScope.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace AppScopes; use Illuminate\Database\EloquentScope; use Illuminate\Database\EloquentModel; use Illuminate\Database\EloquentBuilder; class FinishScope implements Scope { /** * Apply the scope to a given Eloquent query builder. * * @param Illuminate\Database\EloquentBuilder $builder * @param Illuminate\Database\EloquentModel $model * @return void */ public function apply(Builder $builder, Model $model) { return $builder->where('status','finished'); } } |
Let me explain.
1. We defined a class that implements the Illuminate\Database\EloquentScope
interface. This interface requires one to implement the apply
method.
The apply
method is where the query happens.
Now, we can apply this Global Scope to any given model by overriding its boot method and use the addGlobalScope
method.
Let’s try it on the Task Model.
Task Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php namespace App; use App\Scopes\FinishScope; use Illuminate\Database\EloquentModel; class Task extends Model { /** * The "booting" method of the model. * * @return void */ protected static function boot() { parent::boot(); static::addGlobalScope(new FinishScope); } } |
Now that we have added the scope to the Task Model, running Task::all()
will return all finished tasks because it will run this query:
1 |
select * from tasks where status = 'finished'; |
Method 2
Task Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php namespace App; use App\Scopes\FinishScope; use Illuminate\Database\EloquentModel; use Illuminate\Database\EloquentBuilder; class Task extends Model { /** * The "booting" method of the model. * * @return void */ protected static function boot() { parent::boot(); static::addGlobalScope('finish', function(Builder $builder){ $builder->where('status','finished'); }); } } |
This is called an anonymous Global Scope.
If you don’t want to involve the Global Scope in a query, you can just call the withoutGlobalScope method like so:
Task::withoutGlobalScope('finish')->get()
Conclusion
It’s that simple to use Global Scopes in Laravel 5.2
Please let me know if you have any questions or observations in the comments section below:

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016