Laravel change enum type column default value to null by migration

Here we explain how to change enum default value once table created in laravel migration. Change enum column type default value in laravel 8, laravel 9. How to set default enum column value in laravel.

Laravel enum column value not change after table created. How to set default enum column value null after table created by migration


Create a migration with enum data type column

We create a new migration for orders table where enum type column 'status' with default value 'pending'.

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->biginteger('product_id');
            $table->float('price', 8,2);
            $table->enum('status', ['pending','success','cancelled'])->default('pending');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
};

Change enum status column default value null

Here an example for change enum column default value to null in migration. Laravel migration not provide any method for change enum column so, we use custom DB statement for chnage enum type column.

<?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 `orders` CHANGE `status` `status` ENUM('pending','success','cancelled') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL;");
    }
  
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        
    }
};
php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Random tutorial links