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.