Eloquent HasManyDeep drops your constraints when you concatenate
Laravel Eloquent HasManyThrough relationships with unlimited levels
At a glance
- What is it?
- A Laravel package extending HasManyThrough to unlimited intermediate levels, covering many-to-many, polymorphic and composite-key chains. Concatenating existing relationships does not carry their where clauses or soft-delete filters across unless you switch to a second method.
- Who is it for?
- This package earns its place when your model graph is genuinely three or more levels deep, which framework relationships cannot express, and it covers the awkward cases that make hand-written joins unpleasant: polymorphic chains, composite keys and unique results across a join. Read the constraint behaviour before refactoring anything.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Activity is slowing. The repository last received commits 8 months ago.
- What is it written in?
- Mainly PHP, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on November 15, 2026, and from our analysis. They are not legal advice.
Star history
- staudenmeir/eloquent-has-many-deep2,871 ★ · -2
| Repository | 2026-10-04 | 2026-11-15 |
|---|---|---|
| staudenmeir/eloquent-has-many-deep | 2.9k | 2.9k |
-2 Stars in the last 42 days, measured from the daily GitHub snapshot. Compare with other projects
Editorial analysis
Concatenating two relationships discards their scopes
The package offers two ways to define a deep relationship, and the convenient one has a silent failure mode. Given a Country that hasManyThrough Posts and a Post that hasMany Comments, the short form concatenates the two existing relationships and returns everything. The documentation states that constraints from the concatenated relationships are not transferred to the new deep relationship by default. That is not a cosmetic difference. If the inner relationship carried a published filter, or a withTrashed call to include soft-deleted rows, or any scope at all, the deep relationship returns rows the originals excluded, and nothing fails. The opt-in is a second method, which takes the relationships as callable arrays rather than as their return values:
return $this->hasManyDeepFromRelationsWithConstraints([$this, 'posts'], [new Post(), 'comments']);That difference in argument shape is the only thing distinguishing the two calls, which makes it easy to reach for the wrong one.
An unqualified column name will not survive the join
Once constraints do transfer, a second problem appears, and the documentation calls it out directly. When a column name appears in more than one of the joined tables, it has to be qualified. The example given contrasts a constraint written against the qualified name, which selects on a published column in the posts table, against the same constraint unqualified. Inside a single relationship the unqualified form works, because there is only one table in scope. Across a deep join there are several, and the database will either reject the ambiguous reference or resolve it to whichever table it prefers. So the refactoring that introduces a deep relationship is also the moment a previously working constraint stops being safe, and the fix is mechanical once you know to look for it.
One caret constraint spans ten release lines
The install instruction is a single command with a caret constraint, which reads as though any recent version will do. It will not. The package publishes a separate line per supported Laravel major, and the table maps thirteen of them: Laravel 13 to package 1.22, 12 to 1.21, 11 to 1.20, 10 to 1.18, 9 to 1.17, 8 to 1.14, 7 to 1.12, 6 to 1.11, Laravel 5.8 to 1.8, and 5.5 through 5.7 to 1.7. A caret constraint starting at the oldest of those resolves to the newest, which targets the newest Laravel. On an older application that is an unsatisfiable or silently downgraded requirement rather than the version you meant. The table is the only mapping provided, so the version has to be chosen deliberately from it. Windows PowerShell gets its own line, because the caret is an escape character there and the constraint has to be written with four of them.
The version table skips a number, and two lines ship together
Two things in the release history deserve a note. The table has no row for package version 1.19: it goes from 1.18 for Laravel 10 straight to 1.20 for Laravel 11. That is consistent with 1.19 existing for a Laravel version that was skipped or withdrawn, but nothing in the documentation says so, so the gap reads as an omission. The second observation explains the release cadence. The two most recent tags were published on the same day roughly a minute apart, a patch on the 1.21 line and a patch on the 1.22 line. That is what maintaining parallel lines looks like in practice: a fix has to be applied and tagged twice, once per Laravel major, and both tags land within the same minute of each other. Nothing is wrong with it, but it means the release list will show pairs rather than a single ascending sequence.
The custom key example stops inside the second array
The manual definition path takes the related model first, then an array of intermediate models running from the far parent down to the related model, matching how the framework's own through relationship orders its arguments. Custom foreign keys go in the third position and custom local keys in the fourth, and null placeholders stand in wherever you want the convention applied instead. The worked example for that is the one that breaks off. It shows the foreign key array with three entries, one per hop, each commented with the table it belongs to, and then stops partway into the local key array that should follow. The shortest form just above it, a single call passing the related model and the intermediate array, is complete and is enough to get started. For the four-argument form you will need the argument order from the prose, since the example stops before finishing its own payload.
MySQL 5.7 is the only database, and Xdebug is always loaded
The test setup is a compose file with three PHP services on 8.3, 8.4 and 8.5, each from a maintained image, all sharing the working directory and a network with one database service. That database is MySQL 5.7, which reached end of life years ago, and it is the only engine in the matrix. So a package whose entire job is generating multi-table joins is verified against one database version, and nothing in the compose file suggests the others are exercised elsewhere. Two smaller things sit alongside. Every PHP service mounts the same xdebug ini file as a loaded extension, meaning the whole matrix runs with the debugger and its coverage instrumentation active rather than only under a coverage job. And the database service carries a committed root password and a test database name in plain text, which is unremarkable for a throwaway container and still worth recognising as a value that must never be copied into anything real.
Static analysis is configured twice, with a checked-in types directory
Alongside the usual test configuration the repository carries two separate static analysis configuration files, a general one and one specific to type checking, plus a directory of type information committed at the top level and a configuration directory for the package itself. Two analysis configs usually means two jobs at different strictness, which is a reasonable way to keep a baseline clean while raising the bar on new code, but nothing in the visible documentation says which is which. The committed types directory is the more interesting of the three, since checking in stubs rather than deriving them is how a package gives its own generics and magic return types to analysers without every consumer repeating the work. Two badges in the header point at separate workflows, one for the test run and one for static analysis, so both are enforced on every push to the main branch.
Four relationship types, composite keys, and four third-party packages
The scope is wider than the name suggests. Beyond the plain deep has-many and its single-result counterpart, the section list covers many-to-many, morph-many, morph-to-many and morphed-by-many, plus a belongs-to direction, composite keys, access to intermediate and pivot data, constraints on those intermediates, table aliases for self-referencing chains, soft deleting, unique results across the join, and reversed relationship definitions. Several of those exist because a join through several tables multiplies rows, which is the practical reason the package is worth adding. It also concatenates relationships from four external packages, two of which the same author maintains: a merged has-many variant, JSON-backed relationships, tree and graph relationships from an adjacency list package, and a multi-column ships package.
Editorial conclusion
This package earns its place when your model graph is genuinely three or more levels deep, which framework relationships cannot express, and it covers the awkward cases that make hand-written joins unpleasant: polymorphic chains, composite keys and unique results across a join. Read the constraint behaviour before refactoring anything. Concatenating existing relationships discards their where clauses and withTrashed filters by default, so a published-only or exclude-deleted scope can disappear without an error, and the constrained variant needs the relationships passed as callable arrays rather than as return values. Pin the package version to the row matching your Laravel major rather than accepting the caret constraint, since one constraint spans ten release lines. And if you rely on the inner scopes, qualify their column names, because the same unqualified name appearing on two joined tables will not resolve.
Frequently asked questions
What does eloquent-has-many-deep add to Laravel?
It extends HasManyThrough so a relationship can traverse an unlimited number of intermediate models. The example given is Country to User to Post to Comment, which needs one more level than the framework's own through relationship provides, and it supports many-to-many and polymorphic relationships and their combinations.
Which eloquent-has-many-deep version matches my Laravel version?
The README maps them: Laravel 13 to 1.22, 12 to 1.21, 11 to 1.20, 10 to 1.18, 9 to 1.17, 8 to 1.14, 7 to 1.12, 6 to 1.11, Laravel 5.8 to 1.8, and 5.5 through 5.7 to 1.7. The package supports Laravel 5.5 and later.
Why are my where clauses ignored in a deep relationship?
Because constraints on concatenated relationships are not transferred by default. Use hasManyDeepFromRelationsWithConstraints() and pass the relationships as callable arrays to keep them. Once they do transfer, qualify any column name that appears in more than one joined table.
How do I install eloquent-has-many-deep in PowerShell on Windows?
Use the documented PowerShell form of the composer require command, with four caret characters instead of one in the version constraint, because PowerShell treats the caret as an escape character.
Does eloquent-has-many-deep handle soft deletes and composite keys?
Both appear in its section list, alongside table aliases, unique results, intermediate and pivot data, and reversed relationships. The constraints example applies withTrashed() to an inner relationship, which is why the constrained variant exists.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/staudenmeir-eloquent-has-many-deep)Star history chart for your README
[](https://hysenlabs.com/projects/staudenmeir-eloquent-has-many-deep)Was this useful?