Laravel Global Scope Tutorial Example

Laravel Global Scope Tutorial Example

In this post we will give you information about Laravel Global Scope Tutorial Example. Hear we will give you detail about Laravel Global Scope Tutorial ExampleAnd how to use it also give you demo for it if it is necessary.

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.

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: ;

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:

appScopesActiveScope.php



<?php

namespace AppScopes;

use IlluminateDatabaseEloquentBuilder;

use IlluminateDatabaseEloquentModel;

use IlluminateDatabaseEloquentScope;

class ActiveScope implements Scope

{

/**

* Apply the scope to a given Eloquent query builder.

*

* @param IlluminateDatabaseEloquentBuilder $builder

* @param IlluminateDatabaseEloquentModel $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 IlluminateDatabaseEloquentModel;

use AppScopesActiveScope;

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 IlluminateDatabaseEloquentModel;

use AppScopesActiveScope;

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:

Also see:How to Create and Use Query Scope in Laravel Eloquent


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

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

I hope it can help you…

Hope this code and post will helped you for implement Laravel Global Scope Tutorial Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  43  =  51

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US