jeudi 23 avril 2015

Polymorphic relation in Eloquent ORM (Lavarel)

I am trying to achive an inheritance by using polymorphic relations in Eloquent ORM.

My model schema looks like this:

class Section extends Model {
    public function blocks() { // section has many blocks }
}
abstract class Block extends Model {..}
class Exercise extends Block {..}
class Info extends Block {..}

So in my case Section has an array of blocks (and each element could be Exercise or Info).

I tried to tell Eloquent that Section.blocks is an hasMany relation to Block, and Block is morphedTo by it's blockable relation to Exercise or Info, but i failed (and it also doesn't seem like a proper way to do it, because it creates one additional property like $section->blocks[0]->blockable, which should be $section->blocks[0] ).

I also tried to morphTo from Section.blocks right away, but also failed.

Maybe somebody already achived that, and could point me towards right direction.




Aucun commentaire:

Enregistrer un commentaire