Laravel 5.2如何在不丢失数据的情况下更新迁移

Laravel 5.2如何在不丢失数据的情况下更新迁移

问题描述:

我使用的是laravel 5.2,通常会根据项目要求更新数据库,因此我希望这样做不会丢失数据库记录. 我的意思不是说如何为我的数据库设置种子.我的意思是当我的数据库处于活动状态并且想要对其进行更新时,它会引发laravel迁移. 我打算扔 Laravel文档,但是我什么也没发现,所以我希望找人帮助我>

I'm using laravel 5.2 and I usually update my database according to project requirements, so I'd like to do it without losing database records. I don't mean how to seed my database.. I mean when my database is live and I want to update it throw laravel migration. I was going throw Laravel Documentation but I found nothing, so I hope to find somebody help me

由于表中已经有数据,因此您可以创建新的迁移文件来更新表,而不必回滚迁移(这会导致数据丢失).假设您有一个具有列name, email, password的表users.您将数据存储在该表中.然后,您意识到还需要在用户表中添加一个名为mobile_no的新列.为此,您需要创建一个新的迁移文件.命令将是:

Since you already have data in your tables then instead of rollback your migrations (which will cause data losses) you can create new migration files to update your tables. Suppose you have a table users with columns name, email, password. You stored data in that table. Then you realized that you also do need to add a new column named mobile_no to your users table. To do this you need to create a new migration file. Command will be:

php artisan make:migration add_mobile_no_columns_to_users_table --table=users

这样,将创建一个新的迁移文件.在此处设置您的列详细信息,使用php artisan migrate运行迁移,仅此而已.您将在users表中拥有此新列,而不会丢失以前存储的数据.

This way a new migration file will be created. Set your column details there, run the migrations using php artisan migrate and that's all. You'll have this new column in your users table without losing previously stored data.