Laravel - Relationships - Many To Many Polymorphic Defining The Inverse Of The Relationship
Next, on the Tag
model, you should define a method for each of its possible parent models. So, in this example, we will define a posts
method and a videos
method. Both of these methods should return the result of the morphedByMany
method.
The morphedByMany
method accepts the name of the related model as well as the "relationship name". Based on the name we assigned to our intermediate table name and the keys it contains, we will refer to the relationship as "taggable":
morphedByMany(Post::class, 'taggable');
}
/**
* Get all of the videos that are assigned this tag.
*/
public function videos()
{
return $this->morphedByMany(Video::class, 'taggable');
}
}