Renaming Database Tables Using Laravel Migrations

In this post, we are going to rename an existing database table in Laravel 5. Let’s say you have a table called articles and you need to rename it to posts. So, here’s how you would do it.

See Also: How to Add New Column to Existing Table in Laravel

Open your terminal and run the following command.

php artisan make:migration rename_articles_table

This will generate a new PHP file inside your migrations folder (/database/migrations).  To rename the articles table we are going to use rename method.

class RenameArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::rename('articles', 'posts');
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::rename('posts', 'articles');
    }
}

As you can see, the rename method accepts two parameters, from i.e current name and to i.e new name.

Schema::rename($from, $to);

 

0 Comments

Leave a Comment.