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.
Strengths:
Country → User → Post → Comment), which is natively unsupported.HasMany, BelongsTo, ManyToMany, polymorphic, and third-party packages like HasManyMerged or AdjacencyList).hasManyThrough and custom relationships.Weaknesses:
staudenmeir/laravel-adjacency-list (for hierarchical data).topclaudy/compoships (composite keys).spatie/laravel-medialibrary (if polymorphic relationships are used).hasManyThrough with hasManyDeep for complex queries).hasManyDeepFromRelations, hasOneDeep).Country → Comment).select() to limit columns or with() for eager loading intermediate models.whereRaw) could introduce vulnerabilities if not sanitized.DB::enableQueryLog() to verify generated SQL.withTrashed() or withoutTrashed()) for each intermediate model.MorphMany, MorphToMany) may cause type resolution issues if not explicitly defined.Country → Comment a common query, or can it be refactored into a hasManyThrough(User) + manual joins?hasManyThrough + manual joins?CommentRepository) abstract the complexity?use trait).create(), factory()).spatie/laravel-permission (for ManyToMany → HasMany role-permission hierarchies).laravel-nestedset (if using nested sets alongside deep relationships).Model bindings).hasManyThrough chain with hasManyDeep (e.g., Order → Item → Review).$order->items()->whereHas('review', fn($q) => $q->where('rating', '>5'))->get();
to:
$order->reviews()->where('rating', '>5')->get(); // hasManyDeep(Item::class, [Review::class])
composer.json (e.g., "staudenmeir/eloquent-has-many-deep": "1.22" for Laravel 13).latest()) work across databases.Parent → Child → Grandchild).hasManyDeep, then introduce:
hasManyDeepFromRelationsWithConstraints().foreignKey, localKey).scopedByUser()) after the core relationship is stable.Cache::remember()).eloquent-has-many-deep is abandoned, custom implementations may be needed.$this->hasManyDeepFromRelations($a, $b)).How can I help you explore Laravel packages today?