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 access and setup storage files after upload in laravel

How to access and setup storage files after upload in laravel ?

May 23, 2022May 26, 2022

In this article i will show you to access and setup uploaded storage files in laravel. This is strongly recommended that not to use direct URL from storage, laravel internally handle it by creating Symblink but some developers access the files of storage directly without knowing the consequences of website…

Read More
Php Laravel ajax login and register

Laravel ajax login and registration with example

December 4, 2021November 12, 2023

In Laravel 8 there is multiple ways to implement the laravel Ajax login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can…

Read More
Php How to use conditional where in Laravel 8

How to use conditional where clause in Laravel 8 eloquent ?

February 7, 2022October 4, 2022

In Laravel sometimes while creating a eloquent or db builder query you wanted to apply the where 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 when…

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

  • 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

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version