Proper way to save multiple checkboxes to MySQL (Laravel 5.3)

43 views Asked by At

Using Laravel 5.3 and MySQL 5.7.19.

I want to save bunch of options (true/false) to my User model.

These will be used to determine whether certain notifications will be sent to user or not. I will never need to filter Users by these flags. There is a possibility that new flags will be introduced but I don't think the number of flags should ever go above 20-50.

1. My first idea is to save each of the options as separate tinyint(1) column but it will look really, really ugly, even if I prefix these with something like notification_flags_.

2. Second idea is to save the values as integer column and use bitwise operations to deduce the property using PHP. This will take slightly more time to implement but it will look cleaner. Not sure about the performance but probably slower than method 1.

3. Third option is to use another table for flags (let's call this NotficationFlag model) and connect each User to this table via pivot table (using Laravel's manyToMany helper/method (not sure of the proper name)). This option would give me most flexibility (which I don't really need given the nature of the problem) and I think it would be the slowest, but I don't really know.

4. Fourth option would be to have oneToOne relation with NotificationFlag model and in this model to have a column for each flag. This seems to me as the same as option one but a bit nicer, and a bit slower.

What is the best way to do this? It seems to me that first option is the best but something inside me is telling me there must be a nicer way. Is there?

0

There are 0 answers