Weave 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

Extended HasManyThrough for Laravel 5.5+ lets you define relationships with unlimited intermediate models, supporting many-to-many, polymorphic, and combined relationship types. Define deep relationships by concatenating existing ones or specifying keys manually; includes third-party package support...

View on GitHub
Deep Wiki
Context7

Eloquent HasManyDeep extends Laravel's HasManyThrough to support relationships with unlimited intermediate models. Chain existing relationships or define foreign and local keys manually, covering many-to-many, polymorphic, and mixed combinations.

  • Unlimited-depth relationships via relationship concatenation or manual keys
  • Full many-to-many and polymorphic support (MorphMany/MorphToMany)
  • Flexible constraints on intermediate relationships
  • Support for popular third-party packages
  • Compatible with Laravel 5.5+
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.
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
robertmarney/lara-hierarchial-collections
simonschaufi/rule-doc-generator
emissaoja/emissaoja
blackbricksoftware/gravityforms-civicrm-form-processor
al-rahabi/fancy-crud
jinas/dhivatar-component
mltstephane/laravel-quality
bernskiold/laravel-talentlms
bernskioldmedia/laravel-form-architect
youbar/easy-crud
c-point-labs/laravel-hostyourai