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: Introduction weight: 1

This package allows you to add global scopes to models combined with an or condition. It contains additional functionality to disable some or all or-scopes on the fly.

Just add the trait to your eloquent model and then you can use the addGlobalOrScopes method when booting.

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Lacodix\LaravelGlobalOrScope\Traits\GlobalOrScope;

class Post extends Model
{
    use GlobalOrScope;

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

class Scope1 implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        return $builder->whereNull('col1')->where('col2', 1);
    }
}

class Scope2 implements Scope
{
    public function apply(Builder $builder, Model $model)
    {
        return $builder->where('col3', 2);
    }
}
...
Post::query()->where('user_id', 1000)->get();

This results in running the following SQL Query

select * from "posts" where "user_id" = 1000 and (("col1" is null and "col2" = 1) or ("col3" = 2))

For temporary disabling you can use

Post::query()->withoutGlobalOrScopes()->where('user_id', 1000)->get();

what results in a simple

select * from "posts" where "user_id" = 1000
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