Eloquent HasManyDeep Package

Eloquent HasManyDeep Package

https://ift.tt/35HJziH

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


Enjoy this? Get Laravel News delivered straight to your inbox every Sunday.

No Spam, ever. We’ll never share your email address and you can opt out at any time.

programming

via Laravel News https://ift.tt/14pzU0d

December 20, 2019 at 11:01AM