PHP

Implementing Eloquent Global Scopes

Apply automatic query constraints across all queries for a model using Eloquent global scopes, perfect for filtering tenant data or active records.

// 1. Define a global scope class (e.g., app/Scopes/ActiveUserScope.php)
namespace App\Scopes;

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

class ActiveUserScope implements Scope
{
    /**
     * Apply the scope to a given Eloquent query builder.
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->where('is_active', true);
    }
}

// 2. Apply the global scope to your model (e.g., App\Models\User)
namespace App\Models;

use App\Scopes\ActiveUserScope;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The "booted" method of the model.
     * This is where global scopes are registered.
     */
    protected static function booted()
    {
        static::addGlobalScope(new ActiveUserScope);
    }

    // You can also define an anonymous global scope directly:
    /*
    protected static function booted()
    {
        static::addGlobalScope('age_filter', function (Builder $builder) {
            $builder->where('age', '>', 18);
        });
    }
    */
}

// Usage examples
// All queries will automatically include 'where is_active = true'
$activeUsers = App\Models\User::all();

// To remove a global scope for a specific query
$allUsers = App\Models\User::withoutGlobalScope(ActiveUserScope::class)->get();
// Or if it's an anonymous scope with a name:
// $allUsers = App\Models\User::withoutGlobalScope('age_filter')->get();

// Remove all global scopes
$allUsersUnfiltered = App\Models\User::withoutGlobalScopes()->get();
How it works: Global scopes allow you to add automatic constraints to all queries for a given model. This is ideal for scenarios like filtering only active users or restricting data by tenant. You define a scope by implementing the `Illuminate\Database\Eloquent\Scope` interface, and then register it in your model's `booted()` method using `static::addGlobalScope()`. You can temporarily remove a global scope for a specific query using `withoutGlobalScope()` or `withoutGlobalScopes()`. This differs from local scopes which must be explicitly called per query.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs