Laravel Global Scope Tutorial Example

By Hardik Savani April 16, 2024 Category : Laravel

Hi Guys,

In this tutorial, i would like to show you how to define global scope in laravel and how to use global scope in laravel 6 application. i will give you demo to how to create global scope in laravel and how to remove global scope in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11.

Global Scope is a very important concept of laravel 6. using Global Scope you can reuse same eloquent condition in laravel application. i also written tutorial of how to create local scope in laravel 6. Local scope it only for single model. But you define global scope then you can use with all model.

If you want to see how works local scope then you can simply read this tutorial: https://www.itsolutionstuff.com/post/how-to-create-and-use-query-scope-in-laravel-eloquentexample.html.

In this example we will create ActiveScope for getting only active records from model. How to use with multiple models same scope. You can check it step by step bellow:

Create Global Scope File

In this step, we will create new ActiveScope global scope class as like bellow:

app\Scopes\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);

}

}

Define Global Scope in User Model

Here, we will add today scope in our user model. So when we query in controller then we will use that scope in laravel model.

app/User.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use App\Scopes\ActiveScope;

class User extends Model

{

protected $fillable = [

'name','email','password','is_active',

];

protected static function boot()

{

parent::boot();

static::addGlobalScope(new ActiveScope);

}

}

Define Global Scope in Admin Model

Here, we will add today scope in our admin model. So when we query in controller then we will use that scope in laravel model.

app/Admin.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use App\Scopes\ActiveScope;

class Admin extends Model

{

protected $fillable = [

'name','email','password','is_active',

];

protected static function boot()

{

parent::boot();

return static::addGlobalScope(new ActiveScope);

}

}

Get Active Records:

Now you can see how you can use that with your controller file.

You will get only active records using bellow queries:

$users = User::select('*')->get();

$admins = Admin::select('*')->get();

Get All Records:

You will get only add records using bellow queries from users and admins table:

$users = User::select('*')->withoutGlobalScope(ActiveScope::class)->get();

$admins = Admin::select('*')->withoutGlobalScope(ActiveScope::class)->get();

I hope it can help you...

Shares