首页 > 解决方案 > 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();

标签: laraveleloquentpolymorphismrelationship

解决方案


推荐阅读