在项目开发过程中,有时你可能会遇到关于Laravel中迁移的相关问题,比如遇到了一个需要重命名表的实例。那么你会手动更改已经创建的迁移以反映新表名、回滚所有环境并再次迁移它们吗?
下面我们给大家介绍在Laravel中重命名数据库表的一个简单方法。
假设我创建了一个名为“page_info”的表,我想将其重命名为“page_details”。我已经有了一个“create_page_info_table”的迁移,所以现在我只需要创建一个名为“rename_page_info_table”的新迁移,下面是我们要做的。
打开终端,执行以下命令:
1 |
|
然后在新创建的php文件(通常位于/app/database/migration中)中,将文件修改为如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
像这样,就已经重命名了数据库表,而不必手动遍历所有环境和数据库来更改表的名称。
推荐:《PHP教程》http://www.php.cn/course/list/29.html
class RenameAliexpressAppraisesImgTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('aliexpress_appraises_img', function (Blueprint $table) {
Schema::rename('aliexpress_appraises_img', 'aliexpress_appraises_imgs');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('aliexpress_appraises_img', function (Blueprint $table) {
Schema::rename('aliexpress_appraises_imgs', 'aliexpress_appraises_img');
});
}
}