【问题标题】:Laravel Eager Loading Polymorphic RelationshipsLaravel 渴望加载多态关系
【发布时间】:2014-02-24 13:34:07
【问题描述】:

尝试预先加载模型及其相关模型,但相关模型返回 null,即使它具有相关数据。

组模型对 Game 或 Gamer 具有 1:1 的多态性。

组模型关系:

public function groupable()
{
    return $this->morphTo();
}

游戏模型关系:

public function group()
{
    return $this->morphOne('Group', 'groupable');
}

玩家模型关系:

public function group()
{
    return $this->morphOne('Group', 'groupable');
}

查询加载组然后游戏:

$group = Group::whereSubdomain($id)->first();
$game = $group->game;

组返回组,但游戏返回 null。

这是 Groups 表的示例数据库条目:

id    subdomain    groupable_id    groupable_type
5     Starmade     10              Game

这是 Games 表的示例数据库条目:

id    genre    rating
10    7        4.5

不知道我哪里错了没有游戏返回。

【问题讨论】:

    标签: php laravel laravel-4 eager-loading


    【解决方案1】:

    试试这个。这可能会有所帮助。

        public function groupable()
    {
        return $this->morphTo('groupable');
    }
    

    我之前也遇到过这个问题,也许你和我有同样的问题。基本上我所做的是帮助 Laravel 找到它需要查找的列。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-30
      • 2014-04-19
      • 2015-05-16
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      相关资源
      最近更新 更多