Join WhatsApp ChannelJoin Now

How to add Enum Column in Laravel Migration Example

In this article, we will show you How to add Enum Column in Laravel Migration Example. This article will give you simple example of How to add Enum Column in Laravel Migration Example. you will learn How to add Enum Column in Laravel Migration Example.

This article will give you simple example of How to add Enum Column in Laravel Migration Example of How to add Enum Column in Laravel Migration Example in laravel 6, laravel 7 and laravel 8 version.

let’s see bellow examples:-

Step 1 :- 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');
    }
}

Step 2 :- 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');
    }
}

Step 3 :- 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()
    {
          
    }
}

How to add Enum Column in Laravel Migration Example

Recommended Posts