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".