Laravel Advance | Laravel Migration Add Enum Column Example

Raviya Technical
2 min readFeb 6, 2022

--

Here, I will show you the laravel migration add enum column. let’s discuss the laravel migration enum column. let’s discuss laravel migration add enum value. this example will help you how to add an enum column in laravel migration.

Sometimes we take columns like status and you have specified values for it like pending, active, completed, etc. at that time you can choose the enum data type in MySQL. but if you want to add enum data type in laravel migration then how you can add enum data type column in laravel.

However, I will show you examples of how to add enum columns in laravel migration, how to set the default value of enum column using laravel migration, and how to update the value on enum column in laravel migration. you can also use it in laravel 6, laravel 7, and laravel 8 versions.

let’s see the below examples:

Add Enum Data Type Column:

<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateProductsTable extends Migration{/*** Run the migrations.** @return void*/public function up(){Schema::create('products', function (Blueprint $table) {$table->id();$table->string('name');$table->enum('status', ['Pending', 'Wait', 'Active']);$table->timestamps();});}/*** Reverse the migrations.** @return void*/public function down(){Schema::dropIfExists('products');}}

Add Enum Column with Default Value:

<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateProductsTable extends Migration{/*** Run the migrations.** @return void*/public function up(){Schema::create('products', function (Blueprint $table) {$table->id();$table->string('name');$table->enum('status', ['Pending', 'Wait', 'Active'])->default('Pending');$table->timestamps();});}/*** Reverse the migrations.** @return void*/public function down(){Schema::dropIfExists('products');}}

Update Enum Column Values:

we will add a new option called “Completed”, you can see how I added it.

<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class UpdateStatusColumn extends Migration{/*** Run the migrations.** @return void*/public function up(){\DB::statement("ALTER TABLE `products` CHANGE `status` `status` ENUM('Pending','Wait','Active', 'Completed') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Pending';");}/*** Reverse the migrations.** @return void*/public function down(){}}

you can see the below screenshot:

Laravel Advance | Laravel Migration Add Enum Column Example

I hope it can help you….

--

--

No responses yet