Spread the love

Sometime after add the values in enum field we want to update the enum value using laravel migration so that we can work according to requirements. In our last article How to Create Enum Field in Laravel Migration ? we learnt to create the enum and today we are give you a brief idea to update the value of enum field after creating the table.

In laravel while creating table schema for some columns we want to add column type enum field. We can easily add enum column as we add other varchar integer etc columns in laravel. Enum is useful when we want to store specific predefined string in the column only and it restrict to store any type of data.

For example we want to create a column payment_status with values 0 and 1 but suddenly we get a requirement that it can be also pending state so we need to add -1 as well so to accommodate it we need to use ALRER TABLE command of MySQL.

This tutorial will work with all laravel version including laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9.

Here is the syntax

     \DB::statement("ALTER TABLE `movies` CHANGE `status`.`status` ENUM('NEW','OLD','RECENT') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'NEW'");
  

We will use the php artisan command to generate the add column in laravel migration and php artisan migrate command to modify the table.

Let’s understand the Update Enum Field Value using Laravel Migration in laravel step by step

Step 1 : Generate migration file

To generate the migration file we will use the laravel artisan command so open the terminal in project and run below command

php artisan make:migration movies_table

Above command will create a migration file in folder database/migrations

Output: 
Created Migration: 2022_06_30_174050_movies_table

Step 2 : Open generated migration file and update

In the last step we created a migration file using the artisan command and now we wanted to add some more columns to schema of movies table.

so let’s open the file and start editing

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->id();
            $table->string('email')->unique();
            $table->int('user_id');
            $table->string('title');
            $table->string('body')->nullable();
            $table->enum('status',['NEW','OLD']);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
         Schema::dropIfExists('movies');
    }
}

as you can see we have created enum field using $table->enum('status',['NEW','OLD']) syntax.

Step 3 : Update new enum value

Now, in this step we will add a new value RECENT to enum field status so create a new migration as follow

php artisan make:migration alter_movies_table_status_column

If you are only adding new values to enum then this is completely good to use below method but if you are changing all values then i recommend you to check Next one.

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterMoviesTableStatusColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
      \DB::statement("ALTER TABLE `movies` CHANGE `status` `status` ENUM('NEW','OLD','RECENT') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'NEW';");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

This will update your column values but make sure you are only adding new values to enum field because if you are completely changing the values then i recommend you to drop the column first and then update the value otherwise it will throw SQLSTATE[01000]: Warning: 1265 Data truncated for column Error

So to update completely

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AlterMoviesTableStatusColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
       Schema::table('orders', function (Blueprint $table) {
            $table->dropColumn('status');
            
        });
       Schema::table('movies', function (Blueprint $table) {
             
            $table->enum("status",['NEW','OLD','RECENT'])->default("NEW");
        });
     }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

Step 3 : Run Migration

In this step we will execute our migration in database using below command

php artisan migrate

This will create table in database and the output

Output: 
Migrating: 2022_02_15_174050_movies_table
Migrated:  2022_02_15_174050_movies_table (20.08ms)
Migrating: 2022_08_13_115118_alter_movies_table_status_column
Migrated:  2022_08_13_115118_alter_movies_table_status_column (20.08ms)

Leave a Reply