Importance of Database Migration in Laravel
The demand for free, open-source PHP framework is soaring sky-high, so as the empty seats to be fulfilled by niche professionals. We will discuss the database migration present in Laravel, which represents many crucial Laravel interview questions to practice for aspiring candidates looking out to be standalone among others. Migration is a version control mechanism present in Laravel which allows schema of the database to be shared and modified easily. Migrations are paired with the schema builder of Laravel to support the easy build of application's database schema.
So, Why Data Migration is So Crucial?
In Laravel, data migration is beneficial when developers require migrating schema from one server to another. It is also necessary to operate multiple executions such as alter, create, and drop. Users can also rollback with the help of database migration if any undesired operation gets executed. Migration also helps in keeping a record of the creation and alteration of the database.
Generating & Running Migrations
- A Laravel user needs to use the artisan command to make.migration to create a migration.
php artisan make:migration create_users_table
- Then, the newly created migration will be registered in the directory
- Here the migration file name will contain a timestamp that allows Laravel to determine the migration order.
- We can also use
–tableoptions to refer to whether migration will create a new table and indicate the name of the table. These options will be pre-filled in the generated migration stub file with the user-specified table.
- To generate a custom output path for the generated migration, we have to use the --path option while executing the
Any data migration class contains two methods, up() and down(). While the up methods are used to add new columns, tables, and indexes to the database, users can reverse the operations performed by the up methods by using the down method. Now to run all our outstanding data migrations, we have to follow these steps.
- User has to execute the artisan command migrate at first.
php artisan migrate
- Some migration operations may be destructive, which means they may cause data loss. To protect the user data in such situations, the user will be prompted for a confirmation before the command execution. The
--forcethe flag can be used to force the commands to operate without a prompt.
php artisan migrate –force
The Command for Data Migration Rollback
- Developers can use the rollback command to roll back the last batch of l migration operations, which may include multiple migration files.
php artisan migrate:rollback
- With the addition of step command to the rollback, we can rollback a limited number of migrations.
php artisan migrate:rollback --step= X
Advantages of database migration in Laravel
- It is a type of version control for your database in Laravel. They allow us to modify the database schema and stay up to date on the current schema state with artisan commands.
- Database migrations are an excellent way to regain control of this mess. It allows you to:
- It is a deterministic way from our current version of the database to a newer one.
- It helps in re-create a database from scratch.
We can put the number of rollbacks here replacing the X above.