Fabio Santos Fabio Santos - 1 month ago 12x
PHP Question

Laravel 5.2 enums with database

I have a table called role with 5 columns (id,name,role,created_At,update_at).
I need to create a enum ADMINISTRATOR => 'Adinistrador',EMPLOYEE=>'Colaborador'in the databse. And i need access to verifie if is admin, my though is put the query smeting like this:

Is a beter way to do enums with database?


Using enums in db table has one big disadvantage: if you need to add a new value you have to alter the table. If your table has a lot of rows it can be quite time consuming operation. I personally prefer to use tinyint field as foreign key to table that consists of two fields: id, description of the value.

Mysql Column role in your table will store int values 1, 2, 3, 4. Table roles will store rows in a way like: (1, ADMINISTRATOR), (2, User) and so on.


define('ADMINISTRATOR', 1);
define('User', 2);

table->role(ADMINISTRATOR); // search for ADMINISTRATOR