Laravel 8 Eloquent Global Scope Tutorial Example

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share
Laravel 8 Eloquent Global Scope Tutorial Example

Hello Dev’s, Today now in this blog Today, I will show you a shoet example of how to create globle scope in laravel application. Here I will give you a guideto create custom query function in model eloquent by using globle scope in laravel application.

So we can do this easily by using globle query scope in our laravel 8 application. Many tables have same column and aslo same condition to get record then we can use global scope by admin side and user side.

So if we want to create laravel model eloquent scope then we don’t have to write same logic also with where condition again and again.

Sometime, we are working with model query as like get records, also get active records, or get banned users by admin etc. So at this time we will use globle scope. Now here i will give you simple and short example of globle scope and also it will get more active records. So let’s start and see this example here:

Create Global Scope file

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 Scope in User 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 Scope in Admin 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();

        static::addGlobalScope(new ActiveScope);
    }
}

Get Active Records:

So now we can see how we can use that with your controller file.

we will get only active records by using bellow queries:

$users = User::select('*')->get();
  
$admins = Admin::select('*')->get();
Get All Records:

Then we will get only add records by using bellow queries from users and admins table:

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

Read also : How To Set Bcc And Cc Mail Address In Laravel Mail?

I hope it will help you. Also you can follow us on Facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →