How to make specific table migration in laravel ?

We use laravel artisan commands to migrate or rollback the database tables. When we use these commands, all tables are migrated or roll backed at once. Sometime, we have to make some changes in single schema and nothing changes in other. As we know, default rollback and migration will have effect on all tables and data will be lost. Our aim is to make changes to single table by preserving data stored in other tables.

We can achieve such specific table rollback and migration with following approach.

What you have to do is just look at the migrations table in your database. There will be a list of migration file name and batch number value.

Suppose you have following structure,

Suppose we want to make changes in roles table, we have to change it’s migration batch value to 2 manually which is highest among all other batch value.  Now, we have to run following artisan command.

Here, only table with batch value 2 will be rolled back. Now make changes to that table and run following artisan command.

We can use this approach to rollback and migrate more than one table as well so that we can keep data in other tables.

Using Transaction In Laravel

Batch value in the migrations table defines order of the migrations. when you rollback, migrations that are latest or have highest batch value are rolled back at first and then others. So, you can change the value in database and then rollback a particular migration file.

Although it’s not a good idea to change batch number every time because of relationship among the table structure, we can use this case for some cases where single or multiple table rollback doesn’t violates the integrity among the tables.


sagautam5

sagautam5

Computer Enginner, A Laravel Developer,
http://sagargautam.stacklearning.com

Leave a Reply

Your email address will not be published. Required fields are marked *