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().
withGlobalScope). This is a high-value gap for complex query filtering (e.g., "active OR archived" records).Scope interface and Builder pattern, ensuring minimal architectural disruption. The GlobalOrScope trait integrates seamlessly with Eloquent’s lifecycle (booting()/boot()).OrScope). This aligns with modular query design needs.static::addGlobalScope(SoftDeletes::class); // AND
static::addGlobalOrScopes([ActiveScope::class, ArchivedScope::class]); // OR
withoutGlobalOrScopes() and withGlobalOrScopes() mirror Laravel’s conventions, reducing learning curves.WHERE (A OR B) AND C), potentially degrading index usage or requiring higher explain analysis.DB::enableQueryLog() and optimize scopes to use indexed columns.Scope1 OR Scope2 vs. Scope2 OR Scope1).(Scope1 OR Scope2) AND (Scope3 OR Scope4)) require explicit OrScope instantiation, which could obscure intent.ActiveOrArchivedScope).orWhere calls).scopeActiveOrArchived()).Post with published_at OR draft_at).GlobalOrScope trait and test query behavior (SQL output, performance).addGlobalOrScopes().ActiveScope to work with OR).orWhere chains in favor of global OR scopes.withoutGlobalOrScopes() for admin queries").SoftDeletes) remain unchanged.boot() or Builder methods.rector for upgrades if necessary).composer require lacodix/laravel-global-or-scope.DB::enableQueryLog()).booting() registration order.explain and optimize scopes.removedOrScopes() to inspect disabled scopes.WHERE (A OR B) AND C). Mitigate with:
withoutGlobalOrScopes() for high-traffic endpoints.DB::connection()->getPdo()->query("EXPLAIN ...").| Failure Scenario | Impact | Mitigation |
|---|---|---|
| OR scope breaks query logic | Incorrect data returned | Test with DB::enableQueryLog() |
| Performance degradation | Slow queries | Optimize scopes, add indexes, disable scopes |
| Scope conflicts with third-party | Package incompatibility | Isolate models using the package |
| Laravel upgrade incompatibility | Package breaks | Test against Laravel’s upgrade matrix |
| Overuse of OR scopes | Unreadable queries | Enforce code reviews for scope complexity |
How can I help you explore Laravel packages today?