Global Scope in Laravel Tutorial with Example

Laravel 9 Global Scope Example; In this tutorial we are going to share how to define and use Global Scope in Laravel application. Global scopes allow you to add constraints to all queries for a given model. Laravel’s own soft delete functionality utilizes global scopes to only retrieve “non-deleted” models from the database.

Here this example we have added HasActive scope for getting only active records from model step by step example below. So let’s see How to use with same scope with multiple models of laravel.

Laravel Global Scope Steps:

In this tutorial we would cover following topics:

  • Create Global Scope
  • Register Scope in Model
  • Remove Scopes From Query

Create Global Scope

We have to create scope in app\Scopes folder if this folder is not exits in your app you need to create first. Now make a scope named as HasActive.php.

app\Scopes\HasActive.php

<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class HasActive 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);
    }
}

Register Scope in Model

In this step, we’ll define HasActive in User model.

app\User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use App\Scopes\HasActive;

class User extends Authenticatable
{
    use Notifiable, HasApiTokens;

    protected $fillable = [
        'name', 'email', 'password', 'is_active',
    ];

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope(new HasActive);
    }

}

Get Active Records

Now if we get the user then the global scope send only active users.

public function index(Request $request)
{
    $users = User::all();
    return view('users.index', compact('users'));
}

The User::all(), will execute the following SQL query:

select * from `users` where `is_active` = 1

Remove Scope From Query

To remove a global scope for a given query, we can use the withoutGlobalScope method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Scopes\HasActive;
class UserController extends Controller
{

    public function index(Request $request)
    {
        $users = User::withoutGlobalScope(new HasActive)->get();
        return view('users.index', compact('users'));
    }
}

To remove all of the global scopes:

User::withoutGlobalScopes()->get();

To remove some of the global scopes:
User::withoutGlobalScopes([
    HasActive::class, HasCompany::class
])->get();

I hope you enjoy with the laravel global scope example tutorial.

Leave a Comment