Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Update Enum Field Value using Laravel Migration

How to Update Enum Field Value using Laravel Migration ?

Aman Jain, August 13, 2022March 16, 2024

Sometime after add the values in enum field we wanted to Update Enum Field 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.

Syntax to update Update Enum Field Value using Laravel Migration in Mysql Query

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)

Related

Php Laravel Laravel 9 editenumlaravelmigrationupdate

Post navigation

Previous post
Next post

Related Posts

Php How to run specific seeder class in laravel

How to run specific seeder class in laravel 9 ?

February 19, 2022February 22, 2022

In our last article we learnt about how to create seeder in laravel but sometimes we only need to run a specific seeders rather then running all so in this article we will cover to run a specific seeder. We will use a simple example of article class and will…

Read More
Php Laravel whereHas

Laravel whereHas in eloquent Model Example

October 2, 2022March 16, 2024

In this blog post, we’ll take a look at Laravel whereHas in eloquent Model Example and advantages. whereHas method is an important part of the Eloquent ORM. It allows you to add a constraint to a relationship query. whereHas method can be used to filter records based on a relationship….

Read More
Php How to Send Mail with Attachment(PDF, docs,image) in Laravel

How to Send Mail with Attachment(PDF, docs,image) in Laravel 8 /9 ?

May 10, 2022May 10, 2022

Attachment with mail is very important feature of mail by which we can attach any document with our email , for example in some scenarios we want to send the invoice to user mail or some guideline to user after registration. So in this article we will learn to use…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version