- How do I apply multiple global scopes with OR logic in Laravel instead of the default AND?
- Use the `GlobalOrScope` trait in your Eloquent model and call `addGlobalOrScopes([Scope1::class, Scope2::class])` in the `booting()` method. This groups the scopes with OR logic, like `WHERE (scope1 OR scope2)`.
- Can I mix global OR scopes with existing global AND scopes in the same model?
- Yes. The package works alongside Laravel’s native global scopes (AND logic). Just add both `addGlobalScope()` and `addGlobalOrScopes()` in your model’s `booting()` method—they’ll combine naturally.
- How do I disable global OR scopes for a specific query in Laravel?
- Use `withoutGlobalOrScopes()` on your query builder. For example: `Post::query()->withoutGlobalOrScopes()->get()`. This temporarily removes all OR-scoped conditions.
- What Laravel and PHP versions does this package support?
- The package requires **PHP 8.1+** and **Laravel 9+** (tested up to Laravel 12). If you’re on an older version, consider alternatives like manual `orWhere` chains or custom query scopes.
- Will global OR scopes impact query performance or indexing?
- OR conditions can complicate queries (e.g., `WHERE (A OR B) AND C`), potentially reducing index effectiveness. Test with `DB::enableQueryLog()` and ensure scoped columns are indexed. Avoid overusing OR in high-traffic queries.
- Can I nest OR scopes (e.g., `(Scope1 OR Scope2) AND (Scope3 OR Scope4)`) in Laravel?
- Yes, but explicitly. Use `OrScope` to group scopes manually, like `new OrScope([Scope1::class, Scope2::class])`. This gives you control over parenthesized logic, though it requires more boilerplate.
- How do I test models using global OR scopes in PHPUnit?
- Mock the `booting()` method or use `withoutGlobalOrScopes()` in tests to isolate scope behavior. For example: `$model->newQuery()->withoutGlobalOrScopes()->get()` ensures clean test queries.
- Are there alternatives to this package for OR logic in Laravel?
- Yes: (1) **Manual `orWhere`**: Less maintainable but flexible. (2) **Custom query scopes**: Define `scopeActiveOrArchived()` with `orWhere` logic. (3) **Database views**: Overkill for dynamic filtering. This package is the cleanest for reusable OR scopes.
- How do I disable *specific* OR scopes instead of all of them?
- The package currently disables *all* OR scopes with `withoutGlobalOrScopes()`. For granular control, refactor scopes into separate groups or use `OrScope` with conditional logic in your query.
- Does this package work with Laravel’s `SoftDeletes` or other built-in global scopes?
- Absolutely. Built-in scopes like `SoftDeletes` (AND logic) coexist seamlessly with OR scopes. Just ensure your `booting()` method includes both: `static::addGlobalScope(SoftDeletes::class); static::addGlobalOrScopes([...]);`