1

Is it possible to have dynamic relationships in eloquent?

I would like to do the following:

$order = Order::with('commentaar', 'commentaar.auteur')->get();

And in my Commentaar model I have this:

class OrderbonCommentaar extends Eloquent{
    public function auteur()
    {
        if ($this->attributes['fkauteur'] == 'personeel') {
            return $this->belongsTo('Personeel', 'id_auteur');
        } else {
            return $this->belongsTo('ContactPersoon', 'id_auteur');
        }
    }
}

So if the fkauteur column equals 'personeel' it should load the Personeel model and else it should load the Contactpersoon model.

The problem with the code I provided is that the attributes aren't filled when I call the relation.

4

1 回答 1

0

是的,研究多态关系

于 2013-08-12T16:17:49.697 回答