Reputation: 1360
I am trying to use the Many To Many Polymorphic Relationship in laravel for the first time. In the docs for it, for the DB structure it says:
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
What I wonder is what is the difference between the tag_id
and the taggable_id
, what do we store in each?
Upvotes: 0
Views: 135
Reputation: 16191
Using a many-to-many polymorphic relation allows you to have a single list of unique tags that are shared across blog posts and videos.
Having read that, I'd say taggable_id
references either posts.id
or videos.id
, as "taggable" entities. Then, the taggable_type
answers the question - is the taggable_id
a video or a post?
Full table structure for better visualization:
posts
id - integer
name - string
videos
id - integer
name - string
tags
id - integer
name - string
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
Example:
posts
+----+-----------+
| id | name |
+----+-----------+
| 12 | test post |
+----+-----------+
videos
+----+-----------+
| id | name |
+----+-----------+
| 5 | test vid |
+----+-----------+
tags
+----+--------------+
| id | name |
+----+--------------+
| 88 | programming |
+----+--------------+
taggables
+--------+-------------+---------------+
| tag_id | taggable_id | taggable_type |
+--------+-------------+---------------+
| 88 | 12 | post |
+--------+-------------+---------------+
| 88 | 5 | video |
+--------+-------------+---------------+
Upvotes: 2