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, 2022November 17, 2023

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)

Share this:

  • Facebook
  • X

Related

Php Laravel Laravel 9 editenumlaravelmigrationupdate

Post navigation

Previous post
Next post

Related Posts

Javascript Laravel Ajax Autocomplete Using Select2

Laravel Ajax Autocomplete Using Select2

July 17, 2022July 17, 2022

In this article we will learn to use Laravel Ajax Autocomplete Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. Autocomplete search is mostly work of javascript and when…

Share this:

  • Facebook
  • X
Read More
Php Unique Validation in laravel 8

Unique validation on Update in Laravel with example

June 19, 2022November 17, 2023

In this article we will learn to handle the unique validation on update in laravel.As we know Laravel support validation for database operations. Laravel unique validation validates the table column uniqueness using unique validation. In this tutorial we will learn about the unique validation to table columns and also while…

Share this:

  • Facebook
  • X
Read More
Php How to Upload image with preview in Laravel 8 with example

How to Upload image with preview in Laravel 8 with example ?

May 14, 2022August 20, 2022

Upload image with preview in laravel or for any website can be basic requirement like set up a profile picture to providing the documents. Laravel provides robust functionality to upload and process the image with security. Laravel simply use Request file method to access the uploaded file and then we…

Share this:

  • Facebook
  • X
Read More

Leave a ReplyCancel reply

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 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • 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

  • How to Call Controller Method from Another Controller in Laravel ?
  • How to Get Domain Name in Laravel ?
  • How to Append Query String to Route in Laravel ?
  • How to Append URL Query Params to Pagination Laravel ?
  • How to Get Today Records in Laravel ?
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version