Hello, friends today you will learn Laravel migration add enum column example. In this example, I explain laravel migration adds enum value. This example gives you simple laravel migration add enum column example. This article helps you in this example of laravel migration add enum column.
In this example, we will show the laravel migration enum default value. I explain step by step laravel migration enum example. In this post, we discuss how to use enum in laravel migration.
I will show you an example of how to add an enum column in laravel migration and laravel migration change enum values and how to update the value on enum column in laravel migration.
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');
}
}
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');
}
}
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…