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