Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to change data type of column in laravel migration

How to change data type of column in laravel migration ?

Aman Jain, February 20, 2022November 5, 2023

In this blog post we will learn to change data type of column in laravel migration. Laravel covers most of the migration features like add, delete, indexing etc. but to modify the table like renaming column, change data type column to existing table laravel uses a separate package doctrine/dbal. In laravel migration we can change data type of colum to existing table using the method change().

We can install the doctrine/dbal by composer and then we can call function change().

I hope you know about How to make database connection in Laravel 8 ?

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

I am assuming that you have already table movies in database and now we are renaming a column and also you can read How to add column in laravel migration ?

Simple step to change data type of column in laravel migration

Let’s understand the data type of column in laravel migration in laravel step by step

Step 1 : Install doctrine/dbal package

To change the column data type, first we need to install the dependency of doctrine/dbal so install using composer in project root directory

composer require doctrine/dbal

Step 2 : Generate migration file

Once dependency added, now 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 ChangeStatusTypeMoviesTable

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

Output: 
Created Migration: 2022_02_20_064033_change_status_type_movies_table

Step 3 : Open generated migration file and update

In the last step we created a migration file using the artisan command and now we wanted to change the data type of column 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 ChangeStatusTypeMoviesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
    }

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

In the file there is a class named as ChangeStatusTypeMoviesTable and it extends Migration class. ChangeStatusTypeMoviesTable class contains two methods one is up and other one is down.

Up is used to update the database scheme and down method is used to rollback the changes of this migration. As you can see we are creating table movies in up method and dropping the table in down method.

let’s add change the data type of column using change and string method method from our table before updating the database.

 public function up()
 {
        Schema::table('movies', function (Blueprint $table) {
           $table->string('status_new',['1','0'])->change();
        });
 }
 public function down()
 {
        Schema::table('movies', function (Blueprint $table) {
              $table->integer('status_new',11)->change();
        });
 }

Here we used Schema::table to update the table.

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_20_064033_change_status_type_movies_table
Migrated:  2022_02_20_064033_change_status_type_movies_table (72.03ms)

Screenshot Before:

Screenshot 2022 02 20 at 12.06.21 PM
Before laravel migration in phpmyadmin

Screenshot After:

Screenshot 2022 02 20 at 12.17.12 PM
After laravel migration in phpmyadmin

List of modifiers

ModifierDescription
->after('column')Place the column “after” another column (MySQL)
->autoIncrement()Set INTEGER columns as auto-increment (primary key)
->charset('utf8mb4')Specify a character set for the column (MySQL)
->collation('utf8mb4_unicode_ci')Specify a collation for the column (MySQL/PostgreSQL/SQL Server)
->comment('my comment')Add a comment to a column (MySQL/PostgreSQL)
->default($value)Specify a “default” value for the column
->first()Place the column “first” in the table (MySQL)
->nullable($value = true)Allows (by default) NULL values to be inserted into the column
->storedAs($expression)Create a stored generated column (MySQL)
->unsigned()Set INTEGER columns as UNSIGNED (MySQL)
->useCurrent()Set TIMESTAMP columns to use CURRENT_TIMESTAMP as default value
->virtualAs($expression)Create a virtual generated column (MySQL)
->generatedAs($expression)Create an identity column with specified sequence options (PostgreSQL)
->always()Defines the precedence of sequence values over input for an identity column (PostgreSQL)
List of modifiers

Source : Laravel

Related

Php Laravel data typelaravelmigrationphp

Post navigation

Previous post
Next post

Related Posts

Devops How to Install multiple versions of PHP?

How to Install multiple versions of PHP?

September 18, 2021September 21, 2021

Sometimes it’s required to install the different version of PHP rather then the default one of Ubuntu Repository System. To install the different version we will follow the basic steps before installation of PHP Step 1 : Add ppa:ondrej/php to Ubuntu system Open the terminal or ssh connection and then…

Read More
Php How to Delete Cookies in Laravel

How to Delete Cookies in Laravel ?

July 26, 2022July 27, 2022

In this article we will learn to delete cookies in laravel. In our last article we learnt to set and get cookies and how cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. In…

Read More
Php How to Check folder exist and Create a Nested Folder in Laravel

How to Check folder exist and Create Nested Folder in Laravel ?

May 24, 2022May 26, 2022

This article will guide you to Check folder exist and Create Nested Folder in Laravel. To perform the file system based operations like Check folder exist and Create a Nested Folder. we use File class in laravel. Laravel provides inbuilt library to access the file system and we can do…

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