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

Eloquent Power Joins Laravel Package

kirschbaum-development/eloquent-power-joins

Eloquent Power Joins brings Laravel's magic to joins: define joins using your relationship methods, use model scopes in any context, query relationship existence via joins instead of where exists, and sort by related columns or aggregates with less code.

View on GitHub
Deep Wiki
Context7

Eloquent Power Joins brings the "Laravel way" to SQL joins. Instead of writing raw join() clauses, you can join relationships using the same definitions Eloquent already knows, keeping your queries readable and your implementation details hidden. Sort, filter, and aggregate related tables without writing fragile join logic by hand.

  • Join relationships directly via joinRelationship(), including nested relations
  • Use model scopes in different query contexts
  • Query relationship existence with joins instead of where exists
  • Easily sort by columns or aggregations from related tables
  • Supports hasMany, belongsTo, morph, and more
Frequently asked questions about Eloquent Power Joins
How do I replace raw SQL joins with Eloquent Power Joins in my Laravel app?
Use `joinRelationship('relation_name')` instead of `join('table', ...)`. For example, replace `User::join('posts', 'posts.user_id', '=', 'users.id')` with `User::joinRelationship('posts')`. The package automatically resolves the join using your model’s relationship definitions.
Does Eloquent Power Joins support Laravel 13? What about older versions?
Yes, it supports Laravel 11, 12, and 13. For Laravel 10 or below, install version 3.x (`composer require kirschbaum-development/eloquent-power-joins:3.*`). Version 2.x supports Laravel 8. Always check the [Packagist page](https://packagist.org/packages/kirschbaum-development/eloquent-power-joins) for the latest version.
Can I use Eloquent model scopes inside joins with this package?
Absolutely. Pass a closure to apply scopes or constraints. For example: `User::joinRelationship('posts', fn($join) => $join->where('posts.published', true))`. This lets you reuse existing scopes or define ad-hoc conditions directly in the join.
How does `powerJoinHas` differ from Eloquent’s `has()` or `whereExists()`?
`powerJoinHas` replaces `whereExists()` for checking relationship existence but uses joins instead of subqueries, which can improve performance for large datasets. Unlike `has()`, it doesn’t eager-load relationships—it’s purely for filtering. Example: `User::powerJoinHas('posts')->get()` instead of `User::has('posts')->get()`.
Will this package break existing raw SQL queries in my Laravel app?
No, it won’t break existing queries. However, if you rely on raw `join()` calls with custom table aliases or complex SQL logic, you may need to refactor those to use `joinRelationship()`. Test critical queries with `->toSql()` to verify the generated SQL matches expectations.
Can I sort by related table columns (e.g., `posts.title`) with Eloquent Power Joins?
Yes, use `orderByRelationship()` or chain `orderBy()` with the related table’s column. Example: `User::joinRelationship('posts')->orderBy('posts.title')`. For aggregations (e.g., `posts.created_at`), use `orderByRaw()` or `orderBy()` with a subquery.
How do I handle polymorphic relationships (e.g., `morphTo`) with this package?
Polymorphic joins work out of the box. For example, if `Post` has a `morphTo('imageable')`, use `Post::joinRelationship('imageable')`. The package automatically resolves the polymorphic type column (e.g., `imageable_type`). For multi-type relationships, you may need to manually specify the type in the join closure.
Does Eloquent Power Joins support soft deletes (`deleted_at`) in joins?
Yes, it defaults to `deleted_at IS NULL` for soft-deleted models in joins. If you need to include trashed records, use `withTrashed()` on the joined model inside the closure: `User::joinRelationship('posts', fn($join) => $join->withTrashed())`. Always test this behavior in your staging environment.
Are there performance considerations when using joins vs. `whereExists()`?
Joins can be more efficient than `whereExists()` for large datasets because they avoid subqueries. However, overusing joins without proper indexing (e.g., missing foreign keys) or `select()` clauses can lead to Cartesian products or N+1 issues. Always benchmark critical queries with `->toSql()` and `->getQuery()->getBindings()`.
What alternatives exist for cleaner Eloquent joins in Laravel?
Alternatives include raw `join()` calls, query builder macros, or packages like `spatie/laravel-query-builder` for advanced SQL. However, Eloquent Power Joins uniquely integrates with Eloquent’s relationship system, scopes, and Laravel conventions, making it the most seamless choice for apps already using Eloquent heavily.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections