Laravel Extended Relationships
More efficient Eloquent relationship methods for your Laravel models — fewer queries, less duplicated code.
The package ships a HasExtendedRelationships trait with four relationship builders that collapse multiple conventional relationships into a single, efficient query:
| Relationship | What it does |
|---|---|
| BelongsToManyKeys | Define several belongsTo-style relations to the same related model in one method |
| HasManyKeys | Inverse of BelongsToManyKeys — several hasMany-style relations with a single query |
| HasManyArrayColumn | hasMany against a local column storing an array of foreign keys |
| BelongsToArrayColumn | Inverse of HasManyArrayColumn — a model that belongs to entries in an array column |
Why?
Eloquent gives you belongsTo and hasMany, but when the same related model is referenced by several keys — for example created_by, updated_by, and deleted_by all pointing at users — you end up writing three nearly identical relationships that fire three queries. These helpers express that pattern in one method that resolves with a single database query.
Getting started
- Install the package with Composer (see Installation)
- Add the
HasExtendedRelationshipstrait to your model - Call the relationship method that fits your data shape (see Usage)