Polyscope - The agent-first dev environment for Laravel

Eloquent Has Many Deep

staudenmeir/eloquent-has-many-deep image

Eloquent Has Many Deep stats

Downloads
6.4M
Stars
2,586
Open Issues
0
Forks
154

View on GitHub →

Laravel Eloquent HasManyThrough relationships with unlimited levels

Eloquent HasManyDeep for Laravel

The Eloquent HasManyDeep package extends Laravel's Eloquent ORM, allowing developers to effortlessly establish deep relationships using unlimited intermediate models. This package is compatible with Laravel 5.5 and later versions.

Key Features

  • Deep Relationships: Easily define relationships with multiple levels beyond the typical limitations, including hasManyThrough.
  • Support for Various Relationship Types: Handles many-to-many, polymorphic relationships, and their combinations smoothly.
  • Third-Party Integrations: Compatible with relationships defined in several third-party packages.
  • Flexible Relationship Definitions: Relationships can be defined by concatenating existing ones or manually specifying intermediate models and keys.
  • Advanced Features: Supports soft deleting, table aliases, pivot data handling, and obtaining unique results.

Installation

Install the package via composer:

composer require staudenmeir/eloquent-has-many-deep:"^1.7"

For PowerShell users in Windows, like in VS Code, use:

composer require staudenmeir/eloquent-has-many-deep:"^^^^1.7"

Usage

Define deep relationships by either concatenating existing relationships or specifying the details manually. For instance, a multi-level relationship can be set up as follows:

class Country extends Model
{
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
 
public function comments()
{
return $this->hasManyDeepFromRelations($this->posts(), (new Post())->comments());
}
 
public function posts()
{
return $this->hasManyThrough(Post::class, User::class);
}
}

Constraints and custom keys can be applied easily, and integration with third-party packages is supported for extending relationship functionalities.

Advanced Configuration

  • Intermediate and Pivot Data: Fetch intermediate table attributes or pivot data seamlessly.
  • Soft Deleting: Include soft-deleted models in the results with withTrashed().
  • Table Aliases: Useful when the same model appears multiple times in a relationship path.
  • Getting Unique Results: Eliminate duplicate models in results using distinct() or groupBy().

Developer Tools

  • IDE Helper Integration: Works with barryvdh/laravel-ide-helper for accurate model relationship hints.

Contributing

Developers are encouraged to contribute to the package by following the guidelines provided in the repository's CONTRIBUTING and CODE OF CONDUCT files.

This package is a powerful tool for developers looking to leverage complex data relationships in Laravel applications, providing both flexibility and robustness in handling advanced ORM features.

staudenmeir photo

Senior Backend Developer: PHP, MySQL, Laravel | @laravel Core Contributor

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Staudenmeir Eloquent Has Many Deep Related Articles

PhpStorm 2026.1 Released image

PhpStorm 2026.1 Released

Read article
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech