Skip to content
Advertisement

Laravel: Rename table field without deleting data during migration

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement