Situation
I have the following models:
Post
Video
Tag
These are stored in the database tables:
posts
(havingid
andname
columns)videos
(havingid
andname
columns)tags
(havingid
andname
columns)
And also a table taggables
with the columns:
tag_id
(The ID of theTag
)taggable_id
(The ID of thePost
orVideo
)taggable_type
(EitherApp\Post
orApp\Video
)
Both Post
and Video
models have a morphToMany
relationship to Tag
, using:
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
And the Tag
model has the inverse morphedByMany
relationships set with:
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
public function videos()
{
return $this->morphedByMany('App\Video', 'taggable');
}
(So far, this works and it is all exactly as described in the Laravel Docs.)
Now, in addition to this, I have a TagGroup
model that has a One-to-Many
relation with Tag
, so multiple tags can belong to a tag group. (With hasMany('App\Tag')
and belongsTo('App\TagGroup')
using a tag_group_id
column in the tags
table.)
Question
How can I set-up a relationship between the TagGroup
model and the Post
and (seperately) Video
models? (For example, to get all posts that have been tagged with tags of a certain group.)
I know there is hasManyThrough
, but that doesn't seem to work with Many-to-Many Polymorphic relationships as the intermediate. Or am I doing something wrong?
There’s also defining custom intermediate tables with MorphPivot
and ->using()
, but the docs are very unclear about that.
Can this be done without any extra plugins/frameworks?