Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 how to update migration without losing data

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

like image 860
Mohamed Shawky Avatar asked Oct 08 '16 15:10

Mohamed Shawky


People also ask

How do I add a column in Laravel migration without losing data?

database\migration\add_new_column_to_products_table.php Now you can run migrate command to add this new field. Simply you can run below command to auto add a new field. Hope it can help you. Laravel Migration example tutorial, in this tutorial you have learned how to add new column to a table without losing data.

Does php artisan migrate delete data?

Migrate refresh will remove all tables, then reinstall all migrations. So any data will be lost.


1 Answers

Since you already have data in your tables then instead of rolling back your migrations (which will cause existing 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 

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.

like image 100
Mahfuzul Alam Avatar answered Oct 14 '22 12:10

Mahfuzul Alam