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 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

Product Decisions This Supports

  • Schema design: normalize vs. flatten. The package lets Eloquent traverse unbounded relationship chains (Country → User → Post → Comment) without denormalizing tables or hand-writing joins. That removes a false trade-off between "clean schema" and "cheap reads," and lets us defer — or skip — a data-model migration project.
  • Feature feasibility and scoping. Nested-resource APIs ("all comments across a customer's projects"), rollup dashboards, notification fan-out targeting, permission inheritance (User → Role → Permission), and multi-tenant scoping all depend on multi-hop traversal. This package turns those from custom query-layer tickets into one relationship method, which directly shrinks estimate size and scope.
  • Build vs. buy. The alternative is a bespoke join/trait layer that we own and maintain forever. MIT license, ~2.9k stars, CI, code coverage, and PHPStan level 10 make this a cheap, reversible borrow. Adopt by default; only build in-house if we hit a capability gap.
  • Roadmap sequencing. Because relationships can be composed from ones we already have (hasManyDeepFromRelations) or declared manually with explicit keys, we can ship a nested-data feature without first refactoring the models. That decouples feature delivery from platform cleanup.
  • Laravel upgrade planning. The documented version matrix tracks Laravel 5.5 → 13.x, so adopting it does not become a blocker in framework upgrades — a real input to upgrade scheduling.
  • Query-cost decisions. Deep chains compile to multi-join SQL. The PM decision is where we allow them: fine for detail views and admin tooling, questionable for high-traffic list endpoints. This package supports eager loading and constraints, so we can keep N+1 out of the picture rather than abandoning Eloquent for raw SQL.

When to Consider This Package

Adopt when:

  • You have a genuine three-or-more-hop chain that product needs to query as a first-class relationship (eager loading, whereHas, withCount, constraints), not just as a one-off report.
  • The chain mixes relationship types — many-to-many, MorphMany, MorphToMany, MorphedByMany, BelongsTo — or crosses third-party relationships (compoships, adjacency-list trees/graphs, JSON relations).
  • You already have the intermediate relationships defined and want to concatenate them instead of re-declaring every foreign key (note: constraints are not transferred unless you use the WithConstraints variant).
  • You need reverse traversal, soft-delete awareness, unique results, or intermediate/pivot data on the deep relationship.
  • You want HasOneDeep semantics (e.g., "latest comment") without a separate query.

Look elsewhere when:

  • Two hops is enough — native hasManyThrough is sufficient and adds no dependency.
  • The need is analytical aggregation across many dimensions — that belongs in a warehouse, view, or reporting layer, not in Eloquent joins.
  • The endpoint is high-QPS and the chain is long; deep joins will dominate latency. Consider materialized/counter tables first.
  • You are not on Eloquent, or you need rich write-through semantics across the chain — this is fundamentally a read/traversal tool.
  • Vendor-risk tolerance is low. This is effectively a single-maintainer package. The dependents: 0 signal in our data should be verified against Packagist before we treat adoption breadth as evidence; do not cite it as social proof. Mitigate by pinning a version and keeping usage behind relationship methods so removal is a contained change.

How to Pitch It (Stakeholders)

For executives (one paragraph): We can deliver a class of nested-data features — rollups, inherited permissions, cross-entity activity views — without a schema migration or a new internal query library. This is a free, MIT-licensed, widely used Laravel package that slots into our existing models with a one-line install and a few lines per relationship. It is low-commitment: it touches how we read data, not how we store it, and it can be removed with contained effort. The main risk is that it is maintained by a single author, so we will pin the version and isolate usage. Recommendation: pilot it on one feature, measure query cost, then standardize.

For engineering: Add staudenmeir/eloquent-has-many-deep via Composer and use the HasRelationships trait. Two modes: concatenate existing relationships (hasManyDeepFromRelations, with a WithConstraints variant if you need the intermediate where/withTrashed clauses carried through — and qualify column names, since they will now be ambiguous), or declare manually with explicit intermediate models, foreign keys, and local keys, using null placeholders for convention defaults. Pivot tables go in the intermediate array for many-to-many; polymorphic keys are arrays starting with the *_type column; swap foreign/local keys on the right side of a pivot; swap them for BelongsTo hops. It supports eager loading, intermediate/pivot data and constraints, table aliases, soft deletes, unique results, and relationship reversal, and is kept at PHPStan level 10 with CI coverage. The version matrix is documented per Laravel major, so pin to the matching release. Caveat to design around: each deep relationship is a multi-join query — benchmark it on the largest tables and keep the longest chains off hot list endpoints.

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.
uni-method/symfony-lib
tacticmedia/scim-server-bundle
neika-ai/symfony-bundle
mattleads/agent-publishing-bundle
glavweb/framework-bundle
glavweb/extra-bundle
glavweb/api-bundle
glavweb/admin-bundle
adachsoft/api-version-bundle
elsayed85/laravel-api-exceptions
ouhssini/laravel-demo-mode
daworks/ncloud-outbound-mailer-driver
gp247/cms
ignibyte/aic-gates
nexa-merchant/blog
novasa/laravel-language-center
opencage/geocode
adolfoholzer/parameters
livenetworks/ln-starter
cgv/localized-resource