2
votes

Most people probably figure this out on their own but I am a total noob so I will give this question a shot for the sake of other noobs.

I am trying to set up laravel-tagging, a tagging system for Laravel framework, which probably has a similar structure to any other tagging system. It comes with 2 tables:

  1. tagging_tags
  2. tagging_tagged

tagging_tags is where tags are stored.

tagging_tagged is probably where tagged articles are stored, but I'm not sure.

The table tagging_tagged contains a column taggable_id, which doesn't come as a primary key and has no auto increment on it:

Field             Type                Null    Key   Default Extra
'id',             'int(10) unsigned', 'NO',  'PRI', NULL,   'auto_increment'
'taggable_id',    'int(10) unsigned', 'NO',  'MUL', NULL,    
'taggable_type',  'varchar(255)',     'NO',  'MUL', NULL,    
'tag_name',       'varchar(255)',     'NO',         NULL,    
'tag_slug',       'varchar(255)',     'NO',  'MUL', NULL,    

Is taggable_id a foreign key of an article that is tagged? Shouldn't it be tagged_id then? Can anyone point me to some newbie guide that explains tagging system structure or just explain what taggable_id is for?

I apologize if this question is out of place.

2

2 Answers

2
votes

This is Polymorphic Relation and this relation allow a model to belong to more than one other model on a single association.

Imagin you have Post and Comment models and them both can have tags.

You have to use only one table for both.

And the taggable_id column will have the ID value of the post or comment, while the taggable_type column will contain the class name of the owning model

0
votes

Thanks to the whyguy's answer I was able to figure out what taggable_id and taggable_type are and how to use them. As I am writing this, it seems Laravel is one of the few major frameworks that utilize Polymorphic Relations. But it seems very useful in terms of minimizing the amount of code needed to implement a database schema.

For anyone who gets confused by the polymnorphic relations in Laravel, here is a youtube video that I found helpful and here is a piece of Laravel documentation about it.