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.
![Eric L. Barnes photo](https://laravel-news.com/storage/profile-photos/YlkXwoKe1xHNPfmxXdAbBIeCqg9kFE7CtyKySeI1.jpg)
Eric is the creator of Laravel News and has been covering Laravel since 2012.