My application is structured as follows:
Property
has a Many-Many relationship with Manager
, and a Unit
has a One-Many relationship with a Property
, i.e. A manager can manage multiple properties, one property can have multiple manager accounts and one property can have multiple units.
I would like to have a HasManyThrough relationship on the manager to get all his units, so ideally it would look something like: $manager->units
instead of having through loop through each property and call $property->units
on it. Is this possible with the current version of laravel?
managers:
properties:
managers_properties:
units:
Eloquent currently does not have methods for chained relations, other than the hasManyThrough
, that is only applicable to 2 chained hasMany
relations. You should create your own implementation to fetch the related resources. The simplest way is to define an accessor on the Manager
model:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Collection;
/**
* @property-read Collection|\App\Models\Property[] $properties
* @property-read Collection|\App\Models\Unit[] $units
*/
class Manager extends Model
{
public function properties(): BelongsToMany
{
return $this->belongsToMany(Property::class);
}
public function getUnitsAttribute(): Collection
{
return $this->properties
->pluck('units')
->flatten(1)
->unique('id')
->sortBy('id');
}
}
You should now be able to access the related units with $manager->units
assuming $manager instanceof App\Models\Manager
.
Note
Calling $manager->units
does perform at most n + 1
database queries: 1
for fetching n
related properties, and another n
for fetching related units for each returned property. "At most" because the resources might have been loaded already because of previous calls to the accessor.
Note
Calling $manager->units
returns you a Collection
of Unit
models, a format that's equivalent to what you'd get from the magic accessor of a to-many relationship method. However getUnitsAttribute()
is not an actual relationship method (it does not return a relationship object), so it can not be treated as such, whereas Manager::properties()
can be.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With