I am building an app and I have in my app around 20 categories and each category has some custom fields. So a user picks a category and fills in the custom fields. After that I need to store the values in a DB.
What would be the best approach?
I need to be able to filter on these custom fields--like an advanced filter--and performance is key too.
Should I just make for each category a separate table with their custom fields in such case?
There are two options that I can think of.
1) Create a JSON field called something like attributes in your category table. And then store basically an array of Key Values in it. That will present some challenges when querying on attributes though. I know there are ways around it but i've never needed it so I do not know.
2) Create a Category Attributes table in your DB that goes something like this
Then create a Category Attribute model in laravel and define a hasmany relationship where Category has many Category Attributes
then querying your categories would go something like this
$categories = Category::whereHas('CategoryAttributes', function ($query) { $query->where('key', '=', 'color'); $query->where('value','=', 'blue'); })->get();