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
Eloquent Power Joins

Eloquent Power Joins Laravel Package

kirschbaum-development/eloquent-power-joins

Add “Laravel way” joins to Eloquent: join via relationship definitions, reuse model scopes in joined contexts, query relationship existence with joins, and sort by related columns/aggregations—cleaner, more readable join queries with less boilerplate.

View on GitHub
Deep Wiki
Context7

Eloquent Power Joins brings the “Laravel way” to SQL joins by letting you build joins directly from your Eloquent relationships, keeping queries readable while hiding repetitive implementation details. It’s designed for teams who rely on joins for performance and reporting, but still want the ergonomics of scopes, relationship constraints, and expressive sorting.

Use it to replace manual join boilerplate with relationship-aware joins that integrate naturally with your models and query patterns—without sacrificing control.

  • Join tables using relationship definitions instead of raw keys
  • Reuse model scopes across different join/query contexts
  • Query relationship existence via joins (not where exists)
  • Sort by related columns or aggregated values with minimal code
  • Write cleaner, more maintainable queries with fewer leaky details
Frequently asked questions about Eloquent Power Joins
How do I replace manual joins like `join('posts', 'posts.user_id', ...)` with relationship-based joins?
Use `joinRelationship('posts')` on your Eloquent query. The package automatically resolves the relationship definition (e.g., `hasMany`) and generates the correct join clause, including foreign key constraints. For example, `User::joinRelationship('posts')->get()` replaces the manual join syntax entirely.
Can I use model scopes (e.g., `scopeActive()`) on joined tables?
Yes. Apply scopes directly to joined relationships using `joinRelationship('posts')->scoped('active')`. The package integrates with Eloquent’s scope system, letting you reuse existing scopes (e.g., `Post::scopePublished()`) in join contexts without duplicating logic.
Does this package support Laravel 13? What about older versions?
The latest version supports Laravel 11–13. For Laravel 10 or below, use tagged releases (e.g., `3.*` for Laravel <10). The package maintains backward compatibility, but always check the [GitHub releases](https://github.com/kirschbaum-development/eloquent-power-joins/releases) for version-specific notes.
How does `powerJoinHas()` differ from Eloquent’s `whereHas()` or `whereExists()`?
`powerJoinHas()` replaces `whereExists()` with a join-based approach, which can improve performance for large datasets by avoiding subqueries. Unlike `whereHas()`, it doesn’t eager-load relationships—it joins them directly, making it ideal for reporting or complex aggregations where you need joined data in the same query.
Will this package work with soft deletes (`deleted_at`)? Do I need to opt in?
Yes, soft deletes are automatically respected in joins. To include soft-deleted records, use `withTrashed()` on the joined relationship (e.g., `joinRelationship('posts')->withTrashed()`). Without it, soft-deleted rows are excluded by default, matching Eloquent’s standard behavior.
Can I sort by columns from joined tables (e.g., `posts.title`)?
Absolutely. Use `orderByPowerJoins('posts.title')` to sort by related columns. You can also sort by aggregations (e.g., `orderByPowerJoins('posts.created_at', 'desc')`) or even nested relationships (e.g., `orderByPowerJoins('comments.votes.count')`).
Are there performance risks with deeply nested joins (e.g., 3+ levels)?
Nested joins can impact performance, especially with large tables. The package mitigates this with modular methods like `joinRelationshipUsingAlias()` for self-referential tables. Always benchmark complex queries and consider denormalizing data or using eager loading (`with()`) for read-heavy paths.
How do I handle self-referential relationships (e.g., a `Category` tree) with joins?
Use `joinRelationshipUsingAlias('children', 'categories')` to specify table aliases for recursive relationships. This prevents ambiguity in join clauses and ensures the correct foreign keys are used. The package also supports customizing join conditions via callbacks.
Does this package work with polymorphic relationships (e.g., `morphTo`)?
Yes, polymorphic joins are supported. Use `joinRelationship('morphTarget')` as usual, and the package will dynamically resolve the intermediate pivot table (e.g., `morph_to_*` columns). However, sorting or filtering by polymorphic fields requires explicit column names (e.g., `orderByPowerJoins('morph_target_column')`).
What alternatives exist if I need more control over raw SQL in joins?
For advanced SQL needs, you can still use Laravel’s raw query builder (e.g., `DB::table()`) alongside this package. The package is designed to be modular—mix `joinRelationship()` with manual joins or subqueries when necessary. For example, `User::join('posts')->joinRelationship('comments')` combines both approaches.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport