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.
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
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
Please or to participate in this conversation.