Arthur Tarasov Arthur Tarasov - 1 year ago 70
PHP Question

What is taggable_id for in tagging system?

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

is where tags are stored.

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

The table
contains a column
, 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,

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
is for?

I apologize if this question is out of place.

Answer Source

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