【问题标题】:Is it possible to have dynamic relationships in Eloquent?是否可以在 Eloquent 中建立动态关系?
【发布时间】:2013-08-12 11:04:04
【问题描述】:

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 模型。

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

【问题讨论】:

  • 这是不可能的,因为当auteur()被调用时,没有加载任何属性。

标签: php laravel eloquent


【解决方案1】:

是的,请查看polymorphic relationships

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2011-06-09
    相关资源
    最近更新 更多