Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johannesz's avatar

Laravel 5 - Eloquent Models Inheritance (Accessing columns from abstract parent class in derived classes)

Hello.

I'm working on a little project and ran across a problem. (See the ERD for the design).

I want to, for example, access the "name" of the creatures table in the Monster Context.

abstract class Creature extends Model {...}

class Character extends Creature {...}

class Monster extends Creature {...}

If I don't make the parent class (Creature) abstract, I can do a workaround like this:


class Monster extends Creature {
    public function getStatblock() {
        return parent::getStatblock();
    }
}

class Creature extends Model {
    public function getStatblock() {
        $statblock = [];
        $statblock["name"] = $this->creature->name;
        return $statblock;
    }
}

As you can see, I have to resolve the FK of the derived class in the parent class, to get to the actual instance of the object. It looks really odd though and I want to have something more elegant.

ERD

edit: I do not want to use single table inheritance unless I really have to. I would like to maintain a one table per class design.

0 likes
13 replies
Sahbi's avatar

Have you found a solution yet for your problem, because I'm facing the same problem..

andreza.vieira's avatar

I'm facing the same problem.

If I use Polymorphic Relations, I have to insert the ID of the sub tables into the parent table. In this scenario, the Monster and Character tables will not have the "creature_id" anymore. It does no sense for me. Any sugestion?

kobear's avatar

@andreza.vieira what if you abstract the foreign key? Whenever I use polymorphic relations, I don't use the standard naming convention that Laravel uses, I always switch it to "parent" (thus making the linkage with "parent_id" and "parent_type").

andreza.vieira's avatar

@kobear , thanks for the answer. I think I did not understand... Can I use polymorphic relation and insert the parent_id into the sub tables (subclasses)? For example, I would have the tables:

  • Creature (id, name, aligment, hitdice) - without the "type_id" and "ability_id"
  • Monster (id, creature_id, size_id)
  • Character (id, creature_id, race_id)

Thanks.

kobear's avatar

Yes, that would work. Monster and Character would each need to have the relationship like below:

public function creature() 
{
    return $this->hasOne(Creature::class);
}
andreza.vieira's avatar

@kobear I will try, thanks. In this case, is it possible a Creature be a Monster and a Character at the same time? I'm asking because in my project I need something like this.

kobear's avatar

Yes, that is exactly what it can do.

andreza.vieira's avatar

@kobear. Suppose that a Creature has a relationship with the Address table. How to access the address street (field from Address) from a Monster?

I will call getRelations function from the Monster to obtain the Creature. After I will call getRelations from the Creature to obtain the Address. Finnally, I will obtain the street from the Address. I did this way, but my relations are empty. I do not know how to implement the all() function to do all the joins with the tables.

Any suggestion will help. Thanks.

kobear's avatar

Keep in mind that if your Creature->Monster is a hasMany relationship , it is going to return a collection. So, if you try to get the address by using

$monster = App\Monster::find('Dracula');
$monster->creature->address;

will return null because $monster->creature is an array. It should be done via referencing the array element

$monster = App\Monster::find('Dracula');
$monster->creature[0]->address;

Please or to participate in this conversation.