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
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
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
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $9500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media