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 Local Class Scope

Laravel Local Class Scope Laravel Package

mpyw/laravel-local-class-scope

View on GitHub
Deep Wiki
Context7

A tiny macro that reuse a global scope class as a local scope

Frequently asked questions about Laravel Local Class Scope
How do I convert an existing global scope to a local class scope?
Move the scope logic into a class implementing the `Scope` interface, then use `User::scoped(YourScope::class)` instead of the global scope. The package auto-registers the `scoped()` macro, so no additional setup is needed. Ensure your class’s `apply()` method matches the original global scope’s query logic.
Can I pass parameters to my local class scopes like I can with query builder scopes?
Yes. Define constructor parameters in your scope class, then pass them directly: `User::scoped(AgeScope::class, '>', 18)`. The package instantiates the class with the provided arguments, allowing dynamic filtering. This mimics Laravel’s native query builder scope behavior.
Will this work with Laravel 10 or PHP 8.1?
No, the package requires PHP 8.2+ and Laravel 11/12/13/14. For older versions, check past releases or consider alternatives like custom traits. The strict versioning ensures compatibility with modern Laravel features like enums and named arguments.
How do I test a local class scope in isolation?
Mock the `Builder` and `Model` dependencies in your scope’s `apply()` method. Since scopes are classes, you can instantiate them directly and test their logic without hitting the database. Use PHPUnit’s `createMock()` for the query builder and model, then verify the expected `where()` clauses are applied.
Is there a performance impact compared to using closures or global scopes?
The package uses reflection to instantiate scopes, which adds minimal overhead. For high-frequency queries, benchmark with `Benchmark::dd()` in Laravel to compare against closures. If performance is critical, consider caching scope instances or simplifying complex logic.
Can I combine multiple local scopes in a single query?
Yes, chain `scoped()` calls: `User::scoped(ActiveScope::class)->scoped(AgeScope::class, '>', 18)`. Each scope’s `apply()` method runs sequentially, modifying the query builder. This works identically to combining query builder scopes or global scopes.
What happens if Laravel adds native local scopes in a future version?
The package’s design aligns with Laravel’s proposed local scope API, so migration should be straightforward. Monitor the package’s roadmap (last updated June 2026) for deprecation notices. If Laravel implements this natively, you can replace `scoped()` calls with the framework’s built-in method.
How do I share a local scope across multiple models using traits?
Define a trait with a method like `scopeActive()` that uses `scoped(YourScope::class)`. Apply the trait to any model: `use ScopesActive`. This reuses the same scope logic across models while keeping the syntax clean. Example: `User::active()` internally calls `scoped(ActiveScope::class)`.
Does this package support third-party ORMs like October CMS?
No, the package assumes standard Laravel Eloquent and Query Builder conventions. Third-party ORMs may not integrate seamlessly. If you’re using October CMS or another non-Eloquent ORM, evaluate alternatives like custom traits or the base query builder.
What’s the best alternative if I need advanced scope features like conditional logic?
For complex scenarios (e.g., conditional scopes, subqueries), consider custom traits or base classes that extend the `Scope` interface. The package’s simplicity may not cover all edge cases, so you might need to build on top of it. Alternatives like `spatie/laravel-query-builder` offer broader functionality but aren’t scope-specific.
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony