Fabio Santos
Fabio Santos

Reputation: 253

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: table->role(ADMINISTRATOR);

Is a beter way to do enums with database?

Upvotes: 0

Views: 449

Answers (1)

Andrej
Andrej

Reputation: 7504

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.

Laravel

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

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

Upvotes: 1

Related Questions