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