Weave 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 global scopes to Eloquent models combined with OR conditions, so multiple scopes are wrapped in a single or group. Disable some or all or-scopes on the fly with withoutGlobalOrScopes for flexible queries.

View on GitHub
Deep Wiki
Context7

laravel-global-or-scope

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

Documentation

You can find the entire documentation for this package on our documentation site

Installation

composer require lacodix/laravel-global-or-scope

Basic Usage

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

Testing

composer test

Contributing

Please run the following commands and solve potential problems before committing and think about adding tests for new functionality.

composer rector:test
composer insights
composer csfixer:test
composer phpstan:test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

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.
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log
hsluv/hsluv
seampact/codegen
seampact/hooks
seampact/ir
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table