Laravel Enum in DB: How to Add New or Modify Old Values

Enums are a popular field type in Databases to store a pre-defined list of keys. Do you know how to update enum values with Laravel? It's pretty simple!

Defining Enum Field Type in Laravel

You might have a field like this in your migration:

Schema::create('posts', function (Blueprint $table) {
// ...
$table->enum('status', ['Draft', 'Published', 'Archived']);
});

This gives you an option to have an exact list of values on the Database and not something else:


Changing Enum Values - Laravel 9 or Higher

In case you need to change the values, we can simply create a new migration and use the change() method:

Schema::table('posts', function (Blueprint $table) {
$table->enum('status', ['Draft', 'Published', 'Archived', 'Scheduled'])->change();
});

With this migration, you should now have an additional option in your Database:


What About Laravel 8 or Lower?

If you are using Laravel 8 or lower, you will have to install a 3rd party package:

composer require doctrine/dbal

Then, you can use change() method as well:

Schema::table('posts', function (Blueprint $table) {
$table->enum('status', ['Draft', 'Published', 'Archived', 'Scheduled'])->change();
});
avatar

Maybe it's me, but this code doesn't really work, since ENUM has several limitations.

Currently on Laravel 10, with DBAL ^3.7 and getting same error as here: https://github.com/laravel/framework/issues/35096

DB::statement() may be a solution as in this example: https://github.com/laravel/framework/issues/35096#issuecomment-1512754452

avatar
Alexandre Junior da Costa

Can confirm, this article's approach does not work on Laravel 10

avatar

What errors do you get? Just tried and got no errors and enum value was added.

avatar

My bad, wasn't descritive enough. The error itself, is when you try to update the enum field, previously created. For example, let's say you have an enum field, that accepts: ['approved', 'failed']

and you wish to add 'pending' as a possible entry to same column.

->change() will not work by default, on specified versions.

Have you tried changing the enum column?

avatar

Yes. I created enum field. Then created another migrations where additional enum value is added how it is shown in this tutorial and it was added without any errors. You say doesn't work but don't say what it is? What error you get?

avatar

The error is specified in github discussion. As stated on my comment:

getting same error as here: https://github.com/laravel/framework/issues/35096

avatar

Maybe the issue is because you have DBAL package? It isn't needed for some time. Can't reproduce your error.

avatar

Can confirm this works in Laravel 10!

I had a little trouble at first, but removing doctrine/dbal package from the project resolved the issue.

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 59 courses (1057 lessons, total 42 h 44 min)
  • 78 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord

Recent Premium Tutorials