Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Global Or Scope Laravel Package

lacodix/laravel-global-or-scope

Add multiple Eloquent global scopes that are grouped and applied with OR logic instead of the default AND. Use a simple trait to register OR-scopes and optionally disable some or all of them per query with withoutGlobalOrScopes().

View on GitHub
Deep Wiki
Context7

title: Advanced Usage weight: 3

Finally you can use our internal OrScope directly. This can be useful for all cases where you already have multiple scope classes that you want to combine with an or.

use Lacodix\LaravelGlobalOrScope\Scopes\OrScope;

$orScope = new OrScope([new Scope1()), new Scope2())])

$query->withGlobalScope('or_scope', $orScope);

This is exactly what our withGlobalOrScopes method on the query builder does. But with that in mind you can even apply more complex scope combinations like this:

$orScope1 = new OrScope([new Scope1()), new Scope2())])
$orScope2 = new OrScope([new Scope3()), new Scope4())])

$query->withGlobalScope('or_scope1', $orScope1);
$query->withGlobalScope('or_scope2', $orScope2);

This will result in a query applying

(SCOPE1 or SCOPE2) and (SCOPE3 or SCOPE4)

Scope exchange

Sometimes you already have applied one Scope to your model on the classic way: But for some cases in your application you need this scope combined with another by or condition. Just do this:

class Post extends Model
{
    public static function boot(): void
    {
        static::addGlobalScope(Scope1::class);
        
        parent::boot();
    }
}

First we remove the solo Scope1::class and re add it in the second step, combined with a new one via OrScope.

    Post::query()
        ->withoutGlobalScope(Scope1::class) 
        ->withGlobalOrScopes([new Scope1(), new Scope2()]);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle