How to Rename Table Column in Laravel-WebJourney

How to Rename Table Column in Laravel-WebJourney

Are you looking to a tutorial for renaming a column name of a table in laravel. Today I will show you how to rename a table column in laravel. To rename a column we must create a migration file and a library doctrine/dbal. This library is suggested by laravel. So let's do the rest of the task.

 

At first run the below command to install the dbal library:

composer require doctrine/dbal

 

Now we will create a migration file to rename our targeted column name. Execute the below artisan command to create our new migration file. This migration file name can be anything.

Run the following artisan command:

php artisan make:migration rename_name_column_in_users_table

 

After complete this command consider you have a table users and it has a column name. Now you want to rename name into full_name. To do this update the new migration like below.

This file has two methods up() and down(). Use the original table name where you want to rename the column inside Schema::table('users' ). In up() method we will write the logic for renaming the column name. Here renameColumn  has two string first one is old column and second one is the rename column name.

 

We must use the original table name inside Schema::table('your table name')

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('users', function (Blueprint $table) {
            $table->renameColumn('name','full_name');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('users', function (Blueprint $table) {
            //
        });
    }
};

 

Thanks. Hope this will helps you a lot. Please fell free to ask any question about this topic.

 

 

If you like what you are reading, please think about buying us a coffee as a token of appreciation.

Buy Me A Coffee

We appreciate your support and are committed to providing you useful and informative content.

We are thankful for your ongoing support 

 

 

 

You May Like These:

 

Laravel 10 multiple form validation on the same page-WebJourney

 

 Laravel 9 ajax form validation and display error messages

 

How to Create and Use Custom Request for smart validation in Laravel

Tags

  • Share This: