Weave Code
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 Has Many Deep

Eloquent Has Many Deep Laravel Package

staudenmeir/eloquent-has-many-deep

Extend Eloquent’s HasManyThrough to traverse unlimited intermediate models. Define deep relationships by concatenating existing relations or manually specifying models/keys. Supports many-to-many, polymorphic relations, and combinations, plus some third‑party packages.

View on GitHub
Deep Wiki
Context7

Eloquent HasManyDeep extends Laravel’s HasManyThrough to let you define deep Eloquent relationships across unlimited intermediate models. Build queries by concatenating existing relationships or by defining intermediate models and keys manually, with support for complex graph-like schemas.

It works with many-to-many and polymorphic relations (and combinations), while staying compatible with multiple Laravel versions (5.5+).

  • Unlimited-depth relationships beyond HasManyThrough
  • Define deep relations by concatenation or manual keys/models
  • Supports many-to-many and morph relationships
  • Handles mixed combinations of relation types
  • Includes integrations for select third-party packages
Frequently asked questions about Eloquent Has Many Deep
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.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport