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();});
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
Can confirm, this article's approach does not work on Laravel 10
What errors do you get? Just tried and got no errors and enum value was added.
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?
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?
The error is specified in github discussion. As stated on my comment:
Maybe the issue is because you have DBAL package? It isn't needed for some time. Can't reproduce your error.
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.