DEV Community

Irsyad A. Panjaitan
Irsyad A. Panjaitan

Posted on

Rename column with laravel migration

First of all, you need to install package dbal.

composer require doctrine/dbal
Enter fullscreen mode Exit fullscreen mode

When it's done, now you need to make one migration like this

php artisan make:migration rename_column_user_name_to_username_in_users_table --table=users
Enter fullscreen mode Exit fullscreen mode

Now open that file inside migrations folder and start to modify it.

public function up()
{
    Schema::table('users', function (Blueprint $table) {
        $table->renameColumn('user_name', 'username');
    });
}

public function down()
{
    Schema::table('users', function (Blueprint $table) {
        $table->renameColumn('user_name', 'username');
    });
}
Enter fullscreen mode Exit fullscreen mode

Now you can migrate it php artisan migrate

And you're done.

Top comments (0)