Site Admin
Site Admin Founder of MeaningArticles
2003 Views

Laravel Migration Add Enum Column

Hello Dev.

Today i am explained to the how to use laravel migration add enum column in your project. This article will give you simple from laravel migration add enum column example. I will help for you in this example of laravel migration add enum column.

Sometime we take column like status and you have specified values for it like stop, hold, done and active. 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.

I will show you examples of how to add enum column in laravel migration, and 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.

Let's get started Example in Laravel Migration Add Enum Column.


Create category migration

Add Enum Data Type Column

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('category', function (Blueprint $table) {
            $table->id();
            $table->string('cat_name');
            $table->enum('status', ['Stop', 'Hold', 'Done']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('category');
    }
}

Add Enum Column with Default Value

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCategoryTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('category', function (Blueprint $table) {
            $table->id();
            $table->string('cat_name');
            $table->enum('status', ['Stop', 'Hold', 'Done'])->default('Stop');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('category');
    }
}


Update enum value column

I am add to the new value option "Active", you can watch how to add.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class UpdateStatusField extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        \DB::statement("ALTER TABLE `category` CHANGE `status` `status` ENUM('Stop', 'Hold', 'Done', 'Active') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Stop';");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.