knubbe
8 months ago
880
4
Laravel

Eloquent with Morph and HasMany

Posted 8 months ago by knubbe

I have 3 entities (Center, Building and Room) and all 3 has method:

/**
 * @return MorphMany
 */
 public function damages() :MorphMany
 {
     return $this->morphMany(Damage::class, 'damageable');
 }

Also Center hasMany Building, Building hasMany Room and Center hasManyThrough Room.

How to write query to get all damages by center included all damages from building that belong to center and all damages from room that belong to center?

Please sign in or create an account to participate in this conversation.