ItSolutionStuff.com

How to Update Enum Value in Laravel Migration?

By Hardik Savani • April 16, 2024
Laravel

Hello,

In this quick example, let's see how to update enum value in laravel. This article will give you simple example of update enum value in laravel. you can see update enum value in laravel migration. This article goes in detailed on laravel migration change enum field. Follow bellow tutorial step of laravel migration enum change.

Sometime we take column like status and you have specified values for it like pending, active, completed etc. at that time you can choose 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 column in laravel migration, how to set default value of enum column using laravel migration and how to update value on enum column in laravel migration. you can also use it in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

let's see bellow examples:

Add Enum Data Type Column:

<?php

use 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:

<?php

use 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 new option call "Completed", you can see how i added.

<?php

use 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 bellow screen shot:

i hope it can help you....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Migration Add Comment to Column Example

Read Now →

How to add Default Value of Column in Laravel Migration?

Read Now →

Laravel Migration Custom Index Name Example

Read Now →

Laravel Migration Custom Foreign Key Name Example

Read Now →

How to Add Index in Laravel Migration?

Read Now →

How to Add Foreign Key in Laravel Migration?

Read Now →

Laravel Migration - How to Add New Column in Existing Table ?

Read Now →

How to Change Table Name using Laravel Migration?

Read Now →

How to Remove Column from Table in Laravel Migration?

Read Now →

How to Change Column Name and Data Type in Laravel Migration?

Read Now →

How to Create Table using Migration in Laravel?

Read Now →

How to Drop Foreign Key Constraint in Laravel Migration?

Read Now →