Laravel Migration Add Enum Column Example

Laravel Migration Add Enum Column Example

Hello friends today you will learn Laravel migration add enum column example. In this example i explain laravel migration add enum value. This example give you simple laravel migration add enum column example. This artical help for you in this example of laravel migration add enum column.

In this example we will show laravel migration enum default value. I explain step by step laravel migration enum example. In this post we discuss about on how to use enum in laravel migration.

I will show you example of how to add enum column in laravel migration and laravel migration change enum values and how to update value on enum column in laravel migration.

Read Also :  Laravel Blade If Condition Example

Laravel Add Enum Migration

<?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');
    }
}

Laravel Migration Enum 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');
    }
}

How to Update Enum Value in Laravel Migration

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 hope you understand of laravel enum migration and it can help you…

Read Also :  Laravel 8 Create Factory Tinker Example