laravel - laravel 雄辩的关系多态
问题描述
我有一个多态关系,评论:(id,text,commentable_id,commentable_type),图像:(id,标题)和视频:(id,标题)。
视频表与事务表具有多对多关系。
我设法恢复了评论及其关系,例如视频,但我无法恢复问题的标题,从评论表传递,在视频表之后,然后是事项表。
低于我的要求。
class Comment extends Model
{
public function commentable()
{
return $this->morphTo();
}
}
class Video extends Model
{
public function comments()
{
return $this->morphOne(Comment::class, 'commentable');
}
public function matters()
{
return $this->belongsToMany(Matter::class, 'videos_matters');
}
}
class Matter extends Model
{
public function videos()
{
return $this->belongsToMany(Video::class, 'videos_matters');
}
}
Request :
$comments = Comment::with('created_by')
->with('commentable.matters')
->whereDate('created_at', '=', date('Y-m-d'))
->get();
解决方案
推荐阅读
- python - 我的 Jupyter 笔记本的数据保存在哪里
- c++ - 是否可以一步在堆上创建一个 lambda?
- c# - 在 C# 异步代码中使用 MongoDB ClientSessions 和事务是否安全?
- java - Cucumber-JVM 3 - 使用 asMap() 将 DataTable 转换为单个对象
- javascript - 将部分 url 与变量进行比较
- dart - 自定义滚动谷歌颤动中的水平列表视图
- sql - 比较 SQL 中的先前值和当前值
- angular5 - 每次重试时重新计算的动态标头
- ios - UICollectionView(水平CollectionView内)滚动时如何缩小大标题?
- android - [[17.0.0,17.0.0]] 的各种其他库正在请求库 com.google.firebase:firebase-iid,但解析为 16.2.0