Home

1 minute read

Renaming Database Tables in Laravel 4

Tony Lea

Renaming Database Tables in Laravel 4

Lets say you've been digging Laravel Migrations and you run into an instance where you need to rename your table. Would you manually change the migrations you already created to reflect the new table name, rollback all your environments and then migrate them again? Absolutely not, that is the whole reason for using migrations.

Let's say that I created a table called 'page_info' and I wanted to rename it to 'page_details'. I already have a migration for 'create_page_info_table' so now I just need to create a new migration called 'rename_page_info_table', here's how you would do it. Open up terminal and execute the following command:

php artisan migrate:make rename_page_info_table

Then inside your newly created php file (usually located in /app/database/migration), you would modify the file to look like the following:

class RenamePageInfoTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{	
		Schema::rename('page_info', 'page_details');
	}

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

}

And just like that you've renamed your database table without having to manually go through all your environments and databases to change the name of your table.

Gotta love migrations ;)