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