Laravel retrieving morphToMany related children

Viewed 33

I have M-to-M morphToMany relationship:

User::class has morphToMany Building::class through buildable table (Many Buildings can have many Users and many Users can have many Buildings) Building::class is used morphToMany with many classes

building_id | buildable_type | buildable_id
1           | App\Models\User| 1
1           | App\Models\Car | 1

Each Building hasMany Floor::class

How can I select $user->floors and have a list with every floor that applies to the user?

I know I can select $user->building->floors, what if I want to write a direct relationship?

class User extends Model {

public function floors()
{
    return $this->hasManyThrough(
        Floor::class,
        "buildable"
    );
}

}
0 Answers
Related