Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hasManyThrough with an intermediate pivot table

Description:

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?

Tables:

managers:

  • id

properties:

  • id

managers_properties:

  • manager_id
  • property_id

units:

  • id
  • property_id
like image 974
Mauricio Trajano Avatar asked Mar 07 '23 23:03

Mauricio Trajano


1 Answers

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.

like image 68
JJWesterkamp Avatar answered Mar 15 '23 19:03

JJWesterkamp