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 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

A Laravel package to add possibility to use global scopes with an or operation in Eloquent Models.

Frequently asked questions about Laravel Global Or Scope
How do I apply OR logic to multiple global scopes in Laravel Eloquent?
Use the `addGlobalOrScopes([Scope1::class, Scope2::class])` method in your model’s `booting()` method. The package groups them into a single `(scope1 OR scope2)` condition while preserving your existing query logic. No need to manually rewrite SQL.
Can I temporarily disable OR-scopes for specific queries (e.g., exports)?
Yes. Use `withoutGlobalOrScopes()` on your query builder to bypass OR-scopes entirely for that request. This is useful for admin views, exports, or API endpoints where you need raw data. Example: `Model::query()->withoutGlobalOrScopes()->get()`.
Does this package work with Laravel 12? Are there breaking changes?
Yes, it officially supports Laravel 12 (verified via PR #10). The package maintains backward compatibility with Laravel 10/11. No breaking changes exist in v1.2.0, so upgrading is safe if you’re on a supported Laravel version.
Will OR-scopes interfere with my existing global scopes (e.g., SoftDeletes)?
No. Standard global scopes (like SoftDeletes) remain unaffected. OR-scopes are applied *in addition to* them, grouped as `(OR-scopes) AND (standard-scopes)`. Test with `DB::enableQueryLog()` to verify the final SQL structure matches expectations.
How do I test queries with OR-scopes in PHPUnit?
Mock the `GlobalOrScope` trait or use Laravel’s `expectsQuery()` to assert the generated SQL. For example: `Model::query()->get();` should produce `WHERE (condition1 OR condition2) AND ...`. Use `withoutGlobalOrScopes()` in tests where needed to isolate behavior.
Is there a performance impact if I apply OR-scopes to large tables?
OR-scopes add complexity to queries, which *can* impact performance if overused. Benchmark with `DB::enableQueryLog()` to check execution plans. For high-traffic models, consider limiting OR-scopes to critical filters or using database indexes on scoped columns.
Can I conditionally enable/disable OR-scopes based on user roles or tenant IDs?
Yes. Use middleware or query macros to dynamically apply `withoutGlobalOrScopes()` or `addGlobalOrScopes()` based on context. Example: `if ($user->isAdmin()) { Model::withoutGlobalOrScopes()->get(); }` for admin-only queries.
What if I need to combine OR-scopes with other query conditions?
The package integrates seamlessly with Eloquent’s fluent query builder. Your manual `where()` clauses will stack as `WHERE (OR-scopes) AND (your_conditions)`. Example: `Model::query()->where('active', true)->get()` adds `AND active = 1` to the generated SQL.
Are there alternatives to this package for OR-scopes in Laravel?
You could manually chain scopes with `orWhere` or use query macros, but those approaches are less maintainable. This package centralizes OR-logic in a reusable trait, reducing boilerplate. For simple cases, custom traits might suffice, but this offers built-in disable functionality.
How do I migrate from standard global scopes to OR-scopes without breaking existing code?
Start by auditing your models to identify scopes that *should* use OR logic. Gradually replace `addGlobalScopes()` with `addGlobalOrScopes()` for targeted scopes. Use feature flags or environment checks to roll out changes incrementally. Test with `DB::enableQueryLog()` to ensure no regressions.
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