Laravel渴望加载多态关系

编程入门 行业动态 更新时间:2024-10-26 08:32:23
本文介绍了Laravel渴望加载多态关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

试图急于加载模型及其相关模型,但是即使相关模型具有相关数据,相关模型也会返回null.

Trying to eager load a model and it's related model but the related model returns null even though it has related data.

组模型与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;

组返回组,但游戏返回空值.

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

id subdomain groupable_id groupable_type 5 Starmade 10 Game

这是游戏"表的示例数据库条目:

id genre rating 10 7 4.5

不确定我要去哪里出错,没有游戏返回.

Not sure where I am going wrong to have no game returned.

推荐答案

尝试一下.可能有帮助.

Try this. It might help.

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

我太早遇到了这个问题,也许你有和我一样的问题.本质上,我所做的是帮助Laravel查找需要查找的列.

I had that problem too earlier, maybe you have the same prob like me. Essentially what I did was to help Laravel finds what columns it needs to look up for.

更多推荐

Laravel渴望加载多态关系

本文发布于:2023-08-04 06:03:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   关系   多态   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!