Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

josepdecid's avatar

Managing DB enum columns for reusability and avoid DRY

Hey Laracasters! I'm starting my Laravel journey and I have doubts about enums usage in DB columns. I've seen that enums are new in PHP8 so searching for similar solutions leads me to very different answers.

I want to add a role column for my User model that is an enum with a few values, let's say admin, editor, and reader. I started with this migration snippet:

$table->enum('role', ['admin', 'editor', 'reader']);

Yes, this just works, but I'd like to extract this enum to be able to reuse it in any controller, view or anywhere else easily. I've seen people suggesting to move the enum to config and then you can import it from everywhere, so you end up with something like this:

$table->enum('role', config('enums.roles'));

However, I'm concerned that if someday I extend the enum to have a new role it may have some implications. Do I need to create a new migration that does this? Even if it's exactly the same code as the previous migration?

$table->enum('role', config('enums.roles'))->change();

I'd like to hear any opinions from experienced people that faced some pros or cons of using enums, or just using an enum for application code and leaving the DB column as a varchar instead of an enum. Also, I'm totally open to defining roles as an array but keep using the enum type in the DB if you think using an enum doesn't provide any benefit.

I know it's possible to create the roles in a new table, and then foreign key there and so on, but I'm interested in this kind of approach for these columns that are mostly tiny sets of "constants".

0 likes
2 replies
martinbean's avatar
Level 80

@josepdecid By all means use enums in your PHP code, but stay away from them as column types in database migrations. Any time you want to add, change, or remove a case, you can’t do it in place; you have to drop the column and re-create it. So just stick storing enum cases in string columns instead:

$table->string('role')->default(Role::Reader->value);
1 like

Please or to participate in this conversation.