Weave 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 Eloquent Scope As Select

Laravel Eloquent Scope As Select Laravel Package

protonemedia/laravel-eloquent-scope-as-select

Laravel Eloquent Scope as Select lets you add local scopes as select subqueries, so aggregated or computed values from related models come back in a single query. Skip extra database hits and N+1 problems while keeping your models clean and readable.

View on GitHub
Deep Wiki
Context7

Laravel package to add Eloquent scopes as constraints as subquery selects.

Frequently asked questions about Laravel Eloquent Scope As Select
How do I install protonemedia/laravel-eloquent-scope-as-select?
Install it with Composer using composer require protonemedia/laravel-eloquent-scope-as-select. Laravel package auto-discovery registers it, so no manual service provider wiring is normally needed. Then use the package's builder API at the call site where you want a scope rendered as a subquery select. Pin the version and confirm the API in your installed release.
What does Laravel Eloquent Scope as Select actually do?
It lets an existing Eloquent local scope or constraint closure be appended to a parent query as a correlated subquery, usually as a derived column. That keeps the scope logic in one place instead of duplicating it as addSelect, withCount, or raw SQL. It is a query-construction convenience, not a new database feature.
Which Laravel and PHP versions are supported?
The package targets Laravel 9, 10, and 11 on PHP 8.1+ according to the assessment, with MySQL, PostgreSQL, and SQLite as expected databases. Because Eloquent internals change across majors, verify the exact supported matrix for the version you pin. Run your test suite after Laravel upgrades.
How do I use a local scope as a select subquery in Eloquent?
You call the package's scope-as-select API on an Eloquent builder and pass the scope plus an alias for the derived column. The scope's constraints are then compiled into a correlated subquery in the parent SELECT. Start with one representative scope and inspect toSql() before rolling it out broadly.
Does this package replace withCount or cause N+1 problems?
It is not a performance feature by itself; it reduces extra database round trips and N+1 patterns by folding scope logic into the main query. For simple counts or existence checks, native withCount, withExists, and withSum may be faster and clearer. Use it where reusing the scope matters more than raw subquery performance.
Can scopes containing joins, groupBy, having, or orderBy and limit be used as subqueries?
Scopes containing joins, groupBy, having, or orderBy with limit can behave differently inside a correlated subquery than at the top level. The package can render the scope, but the SQL semantics are your responsibility, especially for scalar select versus whereIn or exists contexts. Enumerate such scopes during a spike and compare generated SQL against hand-written equivalents.
How do I debug the generated SQL and bindings for a scope-as-select query?
Capture the parent query's toSql() and bindings after adding the scope, then run EXPLAIN on a realistic data set. This is also the right basis for CI assertions on generated SQL, not just result counts. Binding order is the main fragility when combining with whereRaw, selectRaw, or nested where groups.
Is protonemedia/laravel-eloquent-scope-as-select safe for production performance?
It is architecturally inert at the Eloquent builder layer: no migrations, middleware, queues, or cache behavior. The production concern is the generated SQL, because correlated subqueries can be slow on large tables without proper indexes. Review EXPLAIN plans and keep integration tests that assert SQL and bindings.
Does it work with global scopes and soft deletes?
Global scopes and soft deletes should be checked explicitly against the parent query, because the package compiles scope constraints into a subquery. Qualified columns and explicit aliases are important to avoid ambiguous-column errors across joined tables. Add tests for soft-deleted related models and any global scopes on the subquery model.
Is this package well maintained, and what are the alternatives?
It is a small MIT package with a single maintainer and a narrow codebase, so the bus factor is low but the code is forkable. There is no commercial support path, and ecosystem pressure to track every Laravel major is limited. If your use case is mostly counts, native withCount, withExists, or addSelect may be a sufficient alternative.
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.
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core