Eloquent Has Many Deep
Eloquent Has Many Deep stats
- Downloads
- 6.4M
- Stars
- 2,586
- Open Issues
- 0
- Forks
- 154
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()orgroupBy().
Developer Tools
- IDE Helper Integration: Works with
barryvdh/laravel-ide-helperfor 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.