ItSolutionStuff.com

Laravel Global Scope Tutorial Example

By Hardik Savani • April 16, 2024
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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Eloquent Always Load Model Relation Example

Read Now →

Laravel 10 One to Many Eloquent Relationship Tutorial

Read Now →

Laravel 10 Many to Many Eloquent Relationship Tutorial

Read Now →

How to Add Custom Attribute in Laravel Model?

Read Now →

Laravel Model Disable Primary Key & Auto Increment Example

Read Now →

Laravel 9 One to Many Eloquent Relationship Tutorial

Read Now →

Laravel 9 Eloquent Mutators and Accessors Example

Read Now →

Laravel Eloquent doesntHave() Condition Example

Read Now →

How to Create and Use Query Scope in Laravel Eloquent

Read Now →

How to Use Yajra Datatables in Laravel?

Read Now →

How to Add Flash Message in Laravel?

Read Now →

Laravel Eloquent Group By with Multiple Columns Example

Read Now →

Laravel Eloquent Order By Random Row Example

Read Now →