Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to rename column in laravel migration

How to rename column name in laravel 8 / 9 migration ?

Aman Jain, February 20, 2022August 17, 2022

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 rename colum to existing table using the method renameColumn().

We can install the doctrine/dbal by composer and then we can call function renameColumn(), renameColumn accepts two parameters first as old column name and second new column name.

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 ?

Let’s understand the rename column in laravel migration in laravel step by step

Step 1 : Install doctrine/dbal package

To rename the column 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 RenameStatusMoviesTable

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

Output: 
Created Migration: 2022_02_19_184405_rename_status_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 rename 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;

//till laravel 8
//class RenameStatusMoviesTable extends Migration
return new class 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 RenameStatusMoviesTable and it extends Migration class. RenameStatusMoviesTable 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 rename the column using renameColumn method from our table before updating the database.

 public function up()
 {
        Schema::table('movies', function (Blueprint $table) {
           $table->renameColumn('status','status_new');
        });
 }
 public function down()
 {
        Schema::table('movies', function (Blueprint $table) {
              $table->renameColumn('status_new','status');
        });
 }

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_17_172323_rename_status_movies_table
Migrated:  2022_02_17_172323_rename_status_movies_table (27.20ms)

Screenshot Before:

laravel migration  phpmyadmin
Before laravel migration phpmyadmin

Screenshot After:

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

Related

Php Laravel Laravel 9 laravelmigrationphprename

Post navigation

Previous post
Next post

Related Posts

Php How to Restore Soft Deleted Records in Laravel 9

How to Restore Soft Deleted Records in Laravel 9 ?

June 16, 2022June 15, 2022

In this article we will learn to restore soft deleted records in Laravel. In our recent article How to fetch Soft Deleted Records in Laravel 9 ? we learnt to fetch the records from database which is soft deleted and sometimes we want to restore records that are soft deleted…

Read More
Php How to Call a POST Rest Api in Laravel

How to Call a POST Rest Api in Laravel ?

June 7, 2022June 7, 2022

In this article we will learn to call an post rest api in Laravel. Whenever we want to access the third party data we need to access the data using the APIs, we send a request to another server means outside our application and they respond with preformatted structure. Post…

Read More
Php Facebook Login or Signup in laravel 8 9

How to Implement Facebook Login or Signup in laravel 8 / 9 ?

March 19, 2022March 19, 2022

Facebook login or signup in laravel are demanding from start as it gives user to access the website in single click. Facebook is widely used social media platform therefore most of the users are on it and we can take leverage of it by providing the feature of login and…

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

  • 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

  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version