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

Laravel Eloquent extension for “deep” has-many-through relationships across unlimited intermediate models. Supports many-to-many and polymorphic paths, combinations, and some third-party packages. Define relations by concatenating existing ones or configuring keys manually.

View on GitHub
Deep Wiki
Context7

Laravel Eloquent HasManyThrough relationships with unlimited levels

Frequently asked questions about Eloquent Has Many Deep
How do I define a deep relationship like Country → User → Post → Comment in Laravel Eloquent?
Use `hasManyDeepFromRelations()` to chain existing relationships or manually define keys with `hasManyDeep()`. For example: `$country->hasManyDeepFromRelations(Comment::class, ['users', 'posts'])` or configure foreign/local keys explicitly. The package supports unlimited intermediate models.
Does this package work with Laravel 10/11/12/13?
Yes, it supports Laravel 5.5+. Check the version table in the README for exact package versions per Laravel release (e.g., Laravel 13 uses v1.22). Always install the matching version to avoid compatibility issues.
Will this replace my existing `hasManyThrough` relationships?
No, it extends Eloquent. Use `hasManyDeep` for deep relationships (e.g., 3+ levels) and keep `hasManyThrough` for simpler cases. The package is backward-compatible and won’t break existing code.
How do I handle polymorphic relationships (e.g., Comment → User or Post) with HasManyDeep?
Define polymorphic relationships manually using `morphManyDeep()` or `hasManyDeep()` with explicit `morphType` and `morphKey` constraints. Example: `$model->hasManyDeep(Comment::class, 'comments', 'commentable_id', 'commentable_type')` with `morphMap` if needed.
Can I use this with many-to-many relationships (e.g., User → Role → Permission)?
Yes, the package supports many-to-many deep relationships. Use `hasManyDeep()` with intermediate pivot tables or chain existing `belongsToMany` relations. Example: `$user->hasManyDeepFromRelations(Permission::class, ['roles'])`.
Does HasManyDeep support soft deletes (e.g., `withTrashed()`)?
Yes, but you must explicitly configure `withTrashed()` or `withoutTrashed()` for each intermediate model in the relationship. Example: `$model->hasManyDeep(Comment::class)->withTrashed(['posts', 'users'])`.
Will deep joins impact performance? How can I optimize them?
Deep joins can increase query complexity. Mitigate this by using `select()` to limit columns or `with()` for eager loading intermediate models. Avoid `*` selects and test query times with `DB::enableQueryLog()` to identify bottlenecks.
Does this package work with third-party Eloquent packages like `staudenmeir/laravel-adjacency-list`?
Yes, it integrates with third-party packages. For example, you can combine `hasManyDeep` with adjacency lists for hierarchical data. Check the README for specific examples or open an issue if you need custom support.
How do I debug or profile the SQL generated by HasManyDeep?
Use Laravel’s `DB::enableQueryLog()` to inspect generated SQL. For complex queries, enable Laravel Debugbar or Xdebug. Note that deep joins may produce long SQL strings—simplify relationships or use `toSql()` to verify the query structure.
Is there a way to cache deep relationship queries to improve performance?
Laravel’s built-in query caching may not work with deep relationships. Manually cache results using `remember()` or `cache()` methods on the relationship. Example: `$model->remember(60)->hasManyDeep(Comment::class, [...])` for temporary caching.
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.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony