laravel eloquent relationship polymorphic

╄→гoц情女王★ 提交于 2020-04-17 20:37:12

问题


I have a polymorphic relationship, comments: (id, text, commentable_id, commentable_type ), images :(id, title) and videos :(id, title).

videos table has a many to many relationship with matters table.

I managed to recover the comments and their relationship such as the video but I cannot recover the title of the matter, passing from the comments table, after the videos table and then table of matters.

below my request.

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();

来源:https://stackoverflow.com/questions/60873653/laravel-eloquent-relationship-polymorphic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!