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 use conditional orderby in Laravel 8

How to use conditional orderBy clause in Laravel 8 eloquent ?

February 12, 2022October 4, 2022

In Laravel sometimes while creating a eloquent or db builder query you wanted to apply the orderBy clause on basis of some conditions and to achieve it you may use if else condition but laravel 8 itself provides solution to handle such type of situations using when method. Laravel eloquent…

Read More

What is Laravel 8 middleware ?

November 6, 2021November 6, 2021

Laravel middleware is a mechanism to filter the http request and response. As the name implies that middleware so it’s middle between the request and response. we can change or validate request and response using the Laravel middleware. There is so many use cases of middleware like authentication, validating token,…

Read More
Php How to convert html to pdf in laravel 8 9

How to convert html to pdf in laravel 8 / 9?

May 5, 2022November 6, 2023

Dompdf package has rich features to convert html to PDF in laravel. In this article we will learn to use html to PDF using dompdf package. In most of the website there is need of to convert html to PDF or export the data in PDF format, Dompdf does this…

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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version