Eloquent HasManyDeep
Laravel Eloquent HasManyThrough relationships with unlimited intermediate models and many-to-many support
Repository Health
Technical Analysis
Eloquent HasManyDeep is an extended version of Laravel’s HasManyThrough relationship that removes its two-model limit, letting you define relationships across an unlimited number of intermediate models. It supports many-to-many and polymorphic relationships and every combination of them.
You add a single trait to your models and chain relationships together with a fluent API, or concatenate existing relationships into a deep one. The package handles custom keys, composite keys, table aliases, and intermediate table data while staying faithful to Eloquent conventions.
What You Get
- HasManyDeep and HasOneDeep relationships spanning unlimited intermediate models
- Support for many-to-many, polymorphic, and mixed relationship chains
- Concatenation of existing native relationships into a single deep relationship
- Custom foreign, local, and composite key configuration with table aliases
- Access to intermediate table columns and pivot data along the chain
Common Use Cases
- Reaching deeply nested related records without writing manual joins
- Combining belongsToMany and hasMany hops into one queryable relationship
- Traversing polymorphic relationships across several intermediate models
- Eager loading distant relations with constraints and intermediate data
Under The Hood
Architecture — The core is HasManyDeep.php and HasOneDeep.php relation classes plus a HasRelationships trait that adds hasManyDeep()/hasOneDeep() factory methods to models. Behavior is decomposed into focused traits under src/Eloquent/Relations/Traits (JoinsThroughParents, RetrievesIntermediateTables, SupportsCompositeKeys, HasExistenceQueries, ExecutesQueries, IsConcatenable, IsCustomizable) and concatenation traits (ConcatenatesRelationships, ReversesRelationships) that let existing native relationships be merged into a deep one. A CompositeKey helper and DeepRelation interface round out the model, with an IdeHelperServiceProvider feeding autocompletion.
Tech Stack — PHP 8.3+ built on illuminate/database (Laravel Eloquent), with a companion staudenmeir/eloquent-has-many-deep-contracts package for shared interfaces. The README documents support from Laravel 5.5 upward across tagged versions, and it integrates with several third-party relationship packages.
Code Quality — The project is held to a high bar: PHPStan level 10 static analysis, Codecov-tracked test coverage, and CI across Laravel versions. The many small single-purpose traits keep each concern (joins, composite keys, existence queries, concatenation) isolated and independently testable.
API Design — The public API extends Eloquent naturally — hasManyDeep(Model::class, [Intermediate::class, …]) mirrors hasManyThrough while accepting an array of hops and optional key arrays. Relationship concatenation lets developers compose deep relations from ones they already defined, and thorough README examples cover many-to-many, polymorphic, and custom-key scenarios, though the breadth of options gives it a steeper learning curve than simpler relation helpers.