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.
orWhere scattered across controllers, exports, and admin screens.OrScope wrapper around existing Eloquent Scope classes. The decision is "one more composer dependency" vs. "20–50 lines of internal Eloquent plumbing" — buying wins if you'd otherwise reimplement it in three places; vendoring wins if you need heavier boolean composition.Adopt when:
Scope classes (or closures) and the business rule genuinely is a disjunction, and you want it applied by default to every query on that model.withoutGlobalOrScope, withoutGlobalOrScopes, clearGlobalOrScopes, removedOrScopes — for admin tooling, reporting, and exports. This is usually the part that forces a rewrite of an in-house hack.Look elsewhere (or build in-house) when:
(A∧B) ∨ (C∧¬D). The package composes OR-of-scopes, and stacking OrScope instances only yields (A∨B) ∧ (C∨D). For real expression trees use a specification/query-object pattern or a dedicated filter library.stancl/tenancy or spatie/laravel-multitenancy; a scope helper is not a tenancy strategy.where(function ($q) { ... }) and move on.Adoption gate: 0 dependents means there is no ecosystem signal. Do a source read plus your own integration tests around the exact visibility rules you encode before this sits on a revenue or regulated-data path.
To executives: A small, free, MIT-licensed library that lets us define "who can see which records" once, in one place, instead of re-implementing it in every screen, export, and API endpoint. That directly shrinks the class of bug that becomes a data-exposure incident. It costs nothing to adopt and nothing to reverse: no schema changes, no vendor contract, no migration, no new infrastructure. The honest risk is that it's a single-maintainer open-source project with negligible adoption; the mitigation is that the code is tiny and permissively licensed, so we can absorb it into our own codebase if it's ever abandoned. Recommendation: low-risk, high-leverage for the specific pattern; adopt narrowly and keep an exit path.
To engineering: One composer require, no config files, no migrations. Add the GlobalOrScope trait, register scopes in booting() (or in boot() before parent::boot() — ordering matters), and the model's queries gain (... OR ...) while ordinary global scopes retain AND semantics. Escape hatches cover the usual friction: withoutGlobalOrScope, withoutGlobalOrScopes, removedOrScopes, clearGlobalOrScopes, plus withGlobalOrScopes for ad-hoc composition without the trait, and raw OrScope for stacking (A∨B)∧(C∨D). It wraps standard Illuminate\...\Scope implementations, so there's no proprietary concept to learn and existing scopes are reusable. Two design caveats: no arbitrary nested boolean trees, and the package covers its own tests, not ours — we should add integration tests around the visibility rules we encode. If we ever outgrow it, the file count is small enough to vendor in an afternoon.
How can I help you explore Laravel packages today?