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

Getting Started

Install via Composer:

composer require lacodix/laravel-global-or-scope

Publish the package config (if needed):

php artisan vendor:publish --provider="Lacodix\GlobalOrScope\GlobalOrScopeServiceProvider"

First Use Case: Apply a global OR scope to Eloquent queries:

use Lacodix\GlobalOrScope\GlobalOrScope;

class User extends Model
{
    protected static function booted()
    {
        static::addGlobalScope(new GlobalOrScope('active', fn ($query) => $query->where('is_active', true)));
    }
}

Now all queries on User will automatically include is_active = true unless overridden with withoutGlobalScopes().


Implementation Patterns

Core Usage

  1. Global OR Scopes: Apply OR conditions globally to queries:
    // Override global scope for a specific query
    User::withoutGlobalScopes()->get();
    
  2. Dynamic Scopes: Use closures for dynamic conditions:
    static::addGlobalScope(new GlobalOrScope('search', fn ($query, $scope) => $query->where('name', 'like', '%'.$scope->value.'%')));
    
    Call with:
    User::search('John')->get();
    

Integration Tips

  • Service Providers: Register scopes in boot():
    public function boot()
    {
        User::addGlobalScope(new GlobalOrScope('archived', fn ($query) => $query->where('deleted_at', null)));
    }
    
  • APIs: Useful for filtering soft-deleted or active records by default.
  • Testing: Mock scopes with withoutGlobalScopes() in tests.

Gotchas and Tips

Pitfalls

  • Performance: Global OR scopes apply to all queries. Use sparingly on large tables.
  • Overriding: Forgetting withoutGlobalScopes() can cause unexpected query behavior.
  • Laravel 12: This release adds official support (previously untested). Verify compatibility if upgrading.

Debugging

  • Query Logging: Enable Laravel’s query logging to inspect applied scopes:
    DB::enableQueryLog();
    User::all();
    dd(DB::getQueryLog());
    
  • Scope Isolation: Use withGlobalScopes()/withoutGlobalScopes() to toggle scopes per query.

Extension Points

  • Custom Logic: Extend GlobalOrScope for complex conditions (e.g., multi-table joins).
  • Configuration: Publish the config to adjust default behavior (e.g., scope priority).

Laravel 12 Note

  • No Breaking Changes: The package now officially supports Laravel 12. Test thoroughly if migrating from older versions.
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.
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
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope