有可能在Eloquent中建立动态关系吗


Is it possible to have dynamic relationships in Eloquent?

有可能在雄辩中拥有动态关系吗?

我想做以下事情:

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

在我的Commentaar模型中,我有这样的:

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');
        }
    }
}

因此,如果fkauteur列等于"personeel",则应加载Personeel模型,否则应加载Contactpersoon模型。

我提供的代码的问题是,当我调用关系时,属性没有被填充。

是的,查看多态关系