Ahmad Mobaraki
Ahmad Mobaraki

Reputation: 8188

which method is better to store meta-data (keywords and description) in Mysql (Laravel)?

There are "Articles", "News" tables. I want to allow admin to add meta tags like keywords and description when creating new articles or news. I have used laravel Polymorphic Relations and have two methods in mind to implement :

Method 1

meta_data Table:

id   metable_type   metable_id    meta_type      meta_content

1    news           4             description    php is a good...
2    articles       7             keywords       laravel,relation,tag
3    articles       2             keywords       php,developer,html

.

Method 2

meta_data Table:

id   metable_type   metable_id    meta_type      meta_content

1    news           4             description    php is a good...
2    articles       7             keyword        laravel
2    articles       7             keyword        relation
2    articles       7             keyword        tag
3    articles       2             keyword       php
3    articles       2             keyword       developer
3    articles       2             keyword       html

My Questions:

Is there a better approach than these two methods ? Which method is better?

User should be able to edit kewwords and descriptions for and article or news.

Upvotes: 0

Views: 1375

Answers (1)

bobflux
bobflux

Reputation: 11591

Putting the table name in a column will not allow you to create a foreign key relationship, and this will screw everything. Create a news_attributes and an article_attributes table with proper foreign keys.

meta_type should not be a string. If you have one of several types of attributes, store the types in a separate table, and link them with an integer foreign key. These are likely to be constants, hardcoded in your php code anyway, so why use a slower string?

Now, the second method is normalized and SQL-friendly, since adding/removing keywords is easily done using INSERT and DELETE. This is really the way to go.

The only advantage of storing comma-separated keywords in a column is to be able to put a FULLTEXT index on it. This is the ONLY acceptable scenario.

Upvotes: 2

Related Questions