1

I'm trying to change "name" into "username" without deleting the existing records.

I tried all of these but it just wipes out the data in the table.

php artisan *
     migrate:fresh       Drop all tables and re-run all migrations
     migrate:install     Create the migration repository
     migrate:refresh     Reset and re-run all migrations
     migrate:reset       Rollback all database migrations
     migrate:rollback    Rollback the last database migration
     migrate:status      Show the status of each migration
Vishal Tarkar
  • 808
  • 11
  • 32
Wutame
  • 59
  • 8
  • Possible duplicate of [How can I rename column in laravel using migration?](https://stackoverflow.com/questions/26522292/how-can-i-rename-column-in-laravel-using-migration) – albus_severus Nov 18 '19 at 19:08

3 Answers3

2

Step 1 - php artisan make:migration rename_table
Step 2 - Schema::rename('old_table_name', 'new_table_name');
Step 3 - php artisan migrate

1

For that you have to follow below steps!

1. install Doctrine/dbal

composer require doctrine/dbal

2. create a migration file to rename column name

php artisan make:migration updateTableColumnName

3. add below code to edit your column name

Schema::table('YourTableName', function (Blueprint $table) {
    $table->renameColumn('name', 'username');
});

And Run the migration you have done it without losing your data.

php artisan migrate
Vishal Tarkar
  • 808
  • 11
  • 32
  • Thank you for your prompt response. I did the steps you mentioned, what is the migration command that I should use? Or what do I do next? – Wutame Nov 18 '19 at 18:56
  • just run, php artisan migrate command. after doing above step. and your column name will be changed. – Vishal Tarkar Nov 18 '19 at 18:57
0

suppose you have 'table_name' table and you want to change column 'old_column_name' to 'new_column_name'

php artian make:migration rename_columns_to_table_name_table --table=table_name

and then,

Schema::table('table_name', function ($table) {
    $table->renameColumn('old_column_name', 'new_column_name');
});

run migration,

php artisan migrate
Khem Raj Regmi
  • 2,130
  • 19
  • 21