php - 在 laravel 中使用 morphTo() 多对多多态
问题描述
如何morphTo()
在多对多关系中使用?
考虑一下我有这四个表:
Posts:
- id
- title
Videos:
- id
- title
Tags:
- id
- name
Taggable:
- id
- tag_id
- taggable_type
- taggable_id
在其他多态类型(如 one2one 或 one2many)中,我们可以轻松地在模型中$this->morhphTo()
定义一个taggable()
方法,该方法返回一个App\Post
or的列表App\Video
:
class Tag extends Model
{
public function taggable()
{
return $this->morphTo();
}
}
// So when we call taggable(), it returns either App\Post or App\Video
// But what about using this way in many to many polymorphic relations?
我想要在我的
App\Tag
模型中有这样的东西。这可能吗?
到目前为止,这个问题已经花了我 8 天的时间,我仍然不知道。
解决方案
推荐阅读
- javascript - 如何在 Kendo 日期时间选择器 javascript 中禁用时间范围
- javascript - Ruby on rails -- Jquery 在另一个 post 请求之前没有提交 put 请求
- .net - 将 ContentItem 转换为强类型
- django - Heroku:图像在一段时间后不显示
- java - 保存更改的 TableView 列 Java 的宽度
- javascript - 无法使用 ReactJs 中的按钮功能使 Div 值为空
- google-sheets - Google 表格 IF AND OR 逻辑
- jquery - 通过提供搜索字符串在页面上查找 URL
- javascript - 如果对象匹配更新对象,Redux 检查数组
- r - Rcpp 与普通 C++:标头标志可用?