Laravel 9 Migration Add Enum Column Example

Laravel 9

Nicesnippets

30-07-2022


Laravel 9 Migration Add Enum Column Example

This article will provide some of the most important example laravel 9 migration add enum column. it's simple example of laravel 9 migration enum column. step by step explain laravel 9 migration add enum value. you can see how to add enum column in laravel 9 migration.

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

let's see bellow examples:

Example 1:


Add Enum Data Type Column:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('blogs', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->text('body');

$table->enum('status', ['Pending','Active','Inactive']);

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('blogs');

}

};

Example 2:

Add Enum Column with Default Value:

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

return new class extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('blogs', function (Blueprint $table) {

$table->id();

$table->string('title');

$table->text('body');

$table->enum('status', ['Pending','Active','Inactive'])->default('Pending');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('blogs');

}

};

Output:

It will help you...