- How do I define a deep relationship with 3+ intermediate models in Laravel Eloquent?
- Use `hasManyDeep()` with either concatenated relationships (e.g., `hasManyDeep('comments', 'user.post.author')`) or manual definitions specifying each intermediate model and key mapping. The package supports unlimited hops, so you can chain as many as your schema requires.
- Does this package work with Laravel 10 or 11? What’s the latest version?
- Yes, it supports Laravel 10.x (package v1.18+) and 11.x (v1.20+). Check the [version table](https://github.com/staudenmeir/eloquent-has-many-deep#versions) for exact compatibility. Always use the version matching your Laravel release.
- Can I mix many-to-many and polymorphic relationships in a single deep chain?
- Absolutely. The package explicitly supports combinations of many-to-many (e.g., `belongsToMany`) and polymorphic (e.g., `morphToMany`) relationships in the same deep chain. Define them manually or concatenate existing relationships that include these types.
- How do I avoid N+1 queries when eager-loading deep relationships?
- Use Eloquent’s `with()` method to eager-load deep relationships, just like with standard relationships. Example: `$posts->with('author.comments')->get()`. The package optimizes joins under the hood, but explicit eager loading is still required for performance.
- What’s the difference between concatenating relationships and manual definitions?
- Concatenation chains existing relationships (e.g., `hasManyDeep('comments', 'user.post')`), while manual definitions let you specify every intermediate model, foreign key, and local key explicitly. Use concatenation for simpler schemas or manual definitions for complex or legacy schemas.
- Will this package work with soft deletes? How do I configure it?
- Yes, it supports soft deletes, but you must explicitly enable them with `withTrashed()` on the relationship. Example: `hasManyDeep('comments')->withTrashed()`. Without this, soft-deleted intermediate records will be excluded from results.
- Are there performance concerns with very deep relationships (e.g., 5+ hops)?
- Deep relationships with many intermediate tables can degrade performance due to complex joins. Benchmark with production-like data volumes. For read-heavy workloads, consider caching results or optimizing queries with `select()` to limit columns.
- Does this package integrate with third-party packages like `laravel-adjacency-list`?
- Yes, it works with third-party packages that extend Eloquent relationships. For example, you can chain `hasManyDeep` with adjacency-list-based relationships (e.g., tree structures) or composite-key packages like `compoships` for complex schemas.
- How do I test deep relationships in PHPUnit? Are there edge cases to consider?
- Test deep relationships like any Eloquent relationship, but include edge cases like null foreign keys, circular references, and polymorphic type mismatches. Use factories to simulate complex data hierarchies and verify eager loading behavior with `with()`.
- What’s the migration path from `hasManyThrough` to `hasManyDeep`?
- Start by replacing `hasManyThrough` with `hasManyDeep` for relationships with 3+ intermediate models. Use concatenation for existing relationships or manual definitions for legacy schemas. Pilot with non-critical read-only relationships first to validate performance and behavior.