问题
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