How can I rename column in laravel using migration? How can I rename column in laravel using migration? laravel laravel

How can I rename column in laravel using migration?


You need to create another migration file - and place it in there:

Run

Laravel 4:    php artisan migrate:make rename_stnk_columnLaravel 5:    php artisan make:migration rename_stnk_column

Then inside the new migration file place:

class RenameStnkColumn extends Migration{    public function up()    {        Schema::table('stnk', function(Blueprint $table) {            $table->renameColumn('id', 'id_stnk');        });    }    public function down()    {        Schema::table('stnk', function(Blueprint $table) {            $table->renameColumn('id_stnk', 'id');        });    }}


first thing you want to do is to create your migration file.

Type in your command line

php artisan make:migration rename_stk_column --table="YOUR TABLE" --create

After creating the file. Open the new created migration file in your app folder under database/migrations.

In your up method insert this:

Schema::table('stnk', function(Blueprint $table)    {        $table->renameColumn('id', 'id_stnk');    });}

and in your down method:

    Schema::table('stnk', function(Blueprint $table)    {        $table->renameColumn('id_stnk', 'id);    });}

then in your command line just type

php artisan migrate

Then wollah! you have just renamed id to id_stnk. BTW you can use

php artisan migrate:rollback

to undo the changes. Goodluck


Follow these steps, respectively for rename column migration file.

1- Is there Doctrine/dbal library in your project. If you don't have run the command first

composer require doctrine/dbal

2- create update migration file for update old migration file.Warning (need to have the same name)

php artisan make:migration update_oldFileName_table

for example my old migration file name: create_users_tableupdate file name should : update_users_table

3- update_oldNameFile_table.php

Schema::table('users', function (Blueprint $table) {$table->renameColumn('from', 'to');});

'from' my old column name and 'to' my new column name

4- Finally run the migrate command

php artisan migrate

Source link: laravel document

NOT: This feature supports from laravel 5.x to 8.x version.