The eloquent-has-many-deep package by Jonas Staudenmeir allows Eloquent HasManyThrough relationships with unlimited levels. It supports many-to-many and polymorphic relationships and all their possible combinations.
The package's readme illustrates various types of relationships that this package supports:
- HasMany
- ManyToMany
- MorphMany
- MorphToMany
- MorphedByMany
- BelongsTo
Here's an example from the readme of a HasMany relationship for a complex relationship:
1 2/* 3 Country 4 -> has many 5 User 6 -> has many 7 Post 8 -> has many 9 Comment10*/11 12class Country extends Model13{14 use \Staudenmeir\EloquentHasManyDeep\HasRelationships;15 16 public function comments()17 {18 return $this->hasManyDeep('App\Comment', ['App\User', 'App\Post']);19 }20}21 22// Access country comments23$country->comments();
In the above example, the package uses Eloquent conventions keys, and the package allows you to specify custom keys for local and foreign keys.
Learn More
You can learn more about this package, get full installation instructions, and view the source code on GitHub. Check out the package's readme for in-depth usage of this package.
0 comments:
Post a Comment
Thanks