Skip to content

All topics  /  Laravel

How to Change Enum Type Column In Laravel Migration?

Laravel ·

Teams applying “How to Change Enum Type Column In Laravel Migration?” in a production project can use this time-management reference to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hi Guys,

In this example,I will learn you how to migration add enum column in laravel.you can easy migration add enum column in laravel.

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

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 and laravel 8 version.

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', 'completed', 'running']);
            $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', 'completed', 'running'])->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 "dispute", 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','completed','running','dispute') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Pending';");
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

          
    }
}

It will help you...