Eloquent & Datenzugriff v0

Register Global Or Scopes

Laravel Global Or Scope · A Laravel package to add possibility to use global scopes with an or operation in Eloquent Models.

Version und Seitenübersicht

Sie lesen Version v0. Die aktuelle Dokumentation ist v1.

Commit 2df25a6b45ee Diese Seite auf GitHub

There are multiple ways of registering global scopes that use or condition.

The most common use case is registering a global scope on booting the model. Therefor just add our Trait to your model and finally add the global scopes to your model.

ATTENTION: to make our trait work, the global scopes must be registered before the boot()-method of the base Eloquent model is called. The easy way is using the booting-method of your model.

class Post extends Model
{
    use GlobalOrScope;

    public static function booting(): void
    {
        static::addGlobalOrScopes([Scope1::class, Scope2::class]);
    }
}

But you can also go with the boot method but be sure to run parent::boot() after registering the global scopes.

class Post extends Model
{
    use GlobalOrScope;

    public static function boot(): void
    {
        static::addGlobalOrScopes([Scope1::class, Scope2::class]);
        
        parent::boot();
    }
}

You can also add one single scope, what doesn't make really sense. But maybe you want to use a loop for registering it might be the correct way.

class Post extends Model
{
    use GlobalOrScope;
    
    protected static array $mySpecialScopes = [
        Scope1::class, 
        Scope2::class,
    ];

    public static function boot(): void
    {
        foreach (static::$mySpecialScopes as $scope) {
            static::addGlobalOrScope($scope);
        }
        
        parent::boot();
    }
}

You can use all type of scopes that are know from laravel, with and without keys.

class Post extends Model
{
    use GlobalOrScope;

    public static function boot(): void
    {
        static::addGlobalOrScope(Scope1::class);
        
        static::addGlobalOrScope(new Scope1());
        
        static::addGlobalOrScope('my_scope', new Scope1());
        
        static::addGlobalOrScope('my_closure_scope', function ($query) {
            $query->where('email', 'someone@else.com');
        });
        
        parent::boot();
    }
}

It is also possible to combine the global or scopes with normal global scopes.

class Post extends Model
{
    use GlobalOrScope;

    public static function boot(): void
    {
        static::addGlobalScope(CommonScope::class);
        static::addGlobalOrScopes([Scope1::class, Scope2::class]);
        
        parent::boot();
    }
}

You can get all registered global scopes of a model:

$post->getGlobalOrScopes();

Technisches Erstgespräch

Aus einem Package wird noch kein belastbares Produkt.

Wenn Architektur, Integration oder langfristiger Betrieb entscheidend sind, sprechen wir gern über den vollständigen Anwendungskontext.

Projekt besprechen