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
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
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
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
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
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
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel