- How do I define a deep relationship like Country → User → Post → Comment in Laravel Eloquent?
- Use the `HasManyDeep` trait and chain relationships manually or concatenate existing ones. For example: `$this->hasManyDeep(Comment::class, [User::class, Post::class], 'country_id', ['user_id', 'post_id'], ['user_id', 'post_id'])` specifies the path and keys. The package handles the SQL joins automatically.
- Does this package work with Laravel 13 or 12? What’s the latest version?
- Yes, it supports Laravel 13.x with version `^1.22` and Laravel 12.x with `^1.21`. Always pin the exact minor version (e.g., `1.22.0`) in `composer.json` to avoid compatibility issues. Check the [version table](https://github.com/staudenmeir/eloquent-has-many-deep#versions) for full Laravel support.
- Can I use this with many-to-many or polymorphic relationships?
- Absolutely. The package supports all combinations: `HasMany`, `BelongsTo`, `ManyToMany`, `MorphMany`, `MorphToMany`, and even polymorphic morphs. For example, a `User` can have many `Post` comments via a `ManyToMany` pivot table, then further deepened to `Tag` relationships.
- How do I add constraints (e.g., `where`) to intermediate tables in a deep relationship?
- Qualify column names with the intermediate model’s table alias. For example: `$country->posts()->where('posts.published', true)` ensures the constraint applies to the `posts` table, not the root model. The package propagates constraints through all levels automatically.
- Will this break existing `hasManyThrough` relationships in my app?
- No, it’s a drop-in extension. Replace `hasManyThrough` with `hasManyDeep` where needed, or use the `HasRelationships` trait for backward compatibility. The package doesn’t modify your existing Eloquent models unless you explicitly opt in.
- What if my foreign keys have non-standard names (e.g., `user_id` vs. `author_id`)?
- Specify custom keys in the `hasManyDeep` method. For example: `$this->hasManyDeep(Comment::class, [User::class], 'country_id', ['author_id'])` maps the local key to `author_id` instead of the default `user_id`. This works for all intermediate levels.
- How do I eager load deep relationships to avoid N+1 queries?
- Use Laravel’s `with()` method as usual. For example: `$country->load('posts.comments')` will eager load `posts` and their `comments` in a single query. The package optimizes joins to minimize database load.
- Does this package support soft deletes in intermediate models?
- Yes, but you must explicitly handle soft deletes. Use `withTrashed()` or `withoutTrashed()` on the root relationship. For example: `$country->posts()->withTrashed()->get()` includes soft-deleted `posts` and their intermediate models.
- Are there performance concerns with very deep relationships (e.g., 5+ levels)?
- Deep relationships can impact performance due to complex joins. Optimize by indexing foreign keys, denormalizing data, or caching results for high-traffic endpoints. Test with your actual schema—start with shallow relationships and monitor query execution plans.
- What are the alternatives to `eloquent-has-many-deep` for deep relationships?
- Native `hasManyThrough` is limited to 2 levels. For deeper relationships, consider raw query builder joins or packages like `spatie/laravel-query-builder`, but they lack Eloquent’s ORM features. This package is the most mature solution for Eloquent deep relationships with full relationship type support.