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().
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().
// Override global scope for a specific query
User::withoutGlobalScopes()->get();
static::addGlobalScope(new GlobalOrScope('search', fn ($query, $scope) => $query->where('name', 'like', '%'.$scope->value.'%')));
Call with:
User::search('John')->get();
boot():
public function boot()
{
User::addGlobalScope(new GlobalOrScope('archived', fn ($query) => $query->where('deleted_at', null)));
}
withoutGlobalScopes() in tests.withoutGlobalScopes() can cause unexpected query behavior.DB::enableQueryLog();
User::all();
dd(DB::getQueryLog());
withGlobalScopes()/withoutGlobalScopes() to toggle scopes per query.GlobalOrScope for complex conditions (e.g., multi-table joins).How can I help you explore Laravel packages today?