laravel-5.6 - 继承的多态关系
问题描述
在我的数据库中,我有一个继承的情况。
有一个名为Products的表,其中包含所有类别(如Suits、Footwears和Fabric Packs )之间的共同属性,每个产品应该只是提到的类别之一。
我已经使用该方法$table->morphs('taggable');
创建了适当的列。
并像这样实现了 eloquent 的多态关系:
class Product extends Model
{
public function taggable()
{
return $this->morphTo();
}
}
class Footwear extends Model
{
public function product()
{
return $this->morphMany('Product', 'taggable');
}
}
其他两个班级也是如此。但是当我使用修补程序来验证关系时,它似乎从一侧起作用!
>>> App\Model\Product::find(65)->taggable
[!] Aliasing 'FabricPack' to 'App\Model\FabricPack' for this Tinker session.
=> App\Model\FabricPack {#2928
id: 5,
length: 0.7,
created_at: "2018-07-30 12:19:51",
updated_at: "2018-07-30 12:19:51",
}
我认为App\Model\FabricPack::find(5)->tag
应该返回适当的产品,而是返回 null。
>>> App\Model\FabricPack::find(5)
=> App\Model\FabricPack {#2929
id: 5,
length: 0.7,
created_at: "2018-07-30 12:19:51",
updated_at: "2018-07-30 12:19:51",
}
>>> App\Model\FabricPack::find(5)->tag
=> null
那么,这个问题的任何解决方案?
解决方案
taggable_type值应该是这样App\Model\{subclass}
的。
推荐阅读
- javascript - 为什么这个简单的代码没有运行 if only var
- python - 为什么在这个 for 循环中索引没有增加 2 个位置?
- java - 要解析的文件编码不正确
- mongodb - MongoDB - 是否可以在 db.collection.aggregate() 查询中有条件地合并新文档?
- tensorflow - 如何使用 tensorflow 数据集生成上下文对话数据集的所有组合
- android - 等待另一个颤振命令释放启动锁;尝试在 https://pub.dartlang.org 查找包 english_words 时遇到套接字错误
- elasticsearch - 将 jupiter notebook 与 elasticsearch kibana 链接
- rust - 如何从字符串或数字反序列化无字段枚举?
- sql - 选择其中一列相同的多行
- javascript - 如何用画布制作无限网格?