Eloquent HasManyDeep Package

Eloquent HasManyDeep Package

The Laravel Eloquent HasManyDeep Package is an extended version of Laravel Eloquent’s HasManyThrough that allows relationships with unlimited intermediate models. It includes support for many-to-many and polymorphic relationships.

As an example of how this package is useful to pretend you have a setup where you want to get all comments for all posts, by users in a specific country. It might look something like this:

Country → hasMany → User → hasMany → Post → hasMany → Comment

Granted this gets pretty complicated, but with this package, you can define the relationship like the following:

class Country extends Model
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function comments()
        return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post']);

Then you’ll be able to call Country::first()->comments to get all the comments which keeps the familiar Laravel syntax.

To take this a step further, Povilas Korop from Laravel Daily also created a review video covering this package:

If you’d like to find out more visit the staudenmeir/eloquent-has-many-deep on Github and the readme contains the full documentation.

Filed in: News


Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Fullstack Developer / Architect (w/m/d)
Remote / Munich - Germany
envivo.select GmbH
Medior full stack developer (Laravel)
Deventer (or remote) in the Netherlands, Dutch speaking required.
Senior Full Stack PHP Developer (Laravel)
Senior Laravel Developer
Senior Full-Stack Engineer