Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

Category Storage Best Practice

I have a table of posts. Each post has one or more categories. Same thing with tags. Each post can have one or more tags. I have seen several examples where the tags are simply stored as a comma-separated list in the post table. Can / should the categories be stored the same way? If not, why?

In the previous language I was writing for (ColdFusion), I always stored them in a separate table, one category per database row.

I am wondering what the best practices should be in Laravel. It seems like it may be easier to just store them in the posts table.

TIA, Phil

0 likes
5 replies
Sinnbeck's avatar

I would add them to a separate table like you did in the last project you did. It makes it much easier to search and index. But I would consider making it a many to many relationship with a pivot table, as I imagine you will have a set list of categories that can be used for any post.

martinbean's avatar

@phayes0289 Why would the language/framework change how you store things? “Category” is an entity. Store each category as a separate row in a categories table.

JakeCausier's avatar
Level 6

The best method would be to use relationships, which can assign multiple tags to the posts that you have. You can do the same for categories as well.

To allow for multiple posts to share the same tags (and vice-versa; multiple tags on different posts), you would create an intermediate table that saves which tags are assigned to which posts. In Laravel this is called a pivot table.

You can read more on relationships here: https://laravel.com/docs/9.x/eloquent-relationships

The one specific for this use case would be a many-to-many relationship: https://laravel.com/docs/9.x/eloquent-relationships#many-to-many

phayes0289's avatar

Thank you all... So the proper way to do tags would be to put them in a table. Got it. There are so many video's to there that store them as a comma-separated list.

Thanks.. I have my categories stored in a separate table. Now just need to work on those tags.

JakeCausier's avatar

Yup! They'd work just the same was as your categories.

The pivot table is the key for linking each post to a category or tag they belong to. You can do this either through individual tables for categories and tags, or a more advanced method is using a polymorphic table that defines not only which resource ID to link to, but also the model.

This lets you get really flexible with the types of data you can link, but can get complicated quickly. It's definitely work researching when you're comfortable.

Please or to participate in this conversation.