Updated April 11, 2023
Introduction to Laravel Migration
Laravel Migrations are the version control method for the database. It is done to allow the team to modify the database as well as share it across domains and platforms. Migrations are an integral part of the Laravel procedure and are usually done in conjecture with the Laravel schema builder. This helps to build easily the database schema. Laravel Migrations are an essential part of the Laravel architecture. One of the features which helps in the smooth migration of the database is the Laravel Schema Façade. This allows the database to be modified and manipulated across all the supported database systems of Laravel.
How to Generate Migration?
One of the most pertinent question that hence arises is how to generate the migration?
The easiest way is to use the following query:
php artisan make:migration create_users_table
This allows any new migrations to be placed in the said database or migration directory. It is also important to know that each migration bears a timestamp. This information allows the determination of the order in which the migration has taken place, by Laravel.
One may also use the “table” and “create” options.
This helps to locate the identity of the table and whether the migration will be building a table a new or not.
The query hence will look like this:
php artisan make:migration create_users_table –create=users
php artisan make:migration add_votes_to_users_table –table=users
It is important in this context then, to know the migration structure.
The migration class consists of two procedures:
- Up
- Down
1. Up: The Up procedure allows to create tables, columns and indexes a new supporting the database.
2. Down: The Down procedure allows to do the exact opposite to what was created through the UP procedure.
One may operate the builder to create and make modifications to the tables.
The table may then look like:
How to Write Migration?
All Laravel migrations are executed through the migrate command.
php artisan migrate
There are a few different forms of it though:
Some forms of the migration command may force you to lose your data. A prompt generally appears asking for confirmation of whether you will be continuing with the query.
But you may still run your command without a prompt:
php artisan migrate --force
There will be circumstances where you might have to roll back the latest migration. This reverses the last batch which was migrated.
php artisan migrate:rollback
This roll back may include multiple files.
However, if you wish to migrate a lot more than the last migration, then the STEP query has to be used.
php artisan migrate:rollback –steps=5
This means that the last 5 migrations will be roll backed.
Reset it with the RESET query.
php artisan migrate:reset
Roll back and the Migration can take place in a single command.
php artisan migrate:refresh
// Refresh the database and run all database seeds.
php artisan migrate:refresh --seed
However, like the previous query, you can limit the migrations to a number of your choice.
php artisan migrate:refresh –step=5
This means that the roll back and re-migration happened on the last 5 migrations only.
There are also occasions where you might want to drop all tables and then migrate the database:
schema::drop('users');
schema::dropIfExists('users');
Other forms of queries that one might encounter while migrating a database are:
1. Running migrations which are all outstanding.
php artisan migrate
2. Running Migrations which are all outstanding for a path.
php artisan migrate --path=app/foo/migrations
3. Running migrations which are all outstanding for a package.
php artisan migrate --package=vendor/package
One of the major advantages of using the Laravel framework is the sheer number options it provides to the user. This translates into smoother migrations with a quicker turnaround time.
However, one needs to be careful of what needs to be migrated and in which format. Laravel has a robust system to identify syntax errors.
Basic fundamentals are:
- Anything that goes beyond 767 bytes is signaled as an error. They call it the Syntax error.
- It may also be termed as access violation.
- The code for it is 1071 and the command line access violation is mentioned as “Queryexception”.
What are the benefits one gets with Laravel Migrations?
To begin with it is an easy process.
- All you need to do is create a migration file, by using a simple one-line command.
- Execute the migration, by using another command.
Lo and behold the Migration is complete.
As stated before, Laravel goes ahead and puts a time stamp on them. Hence, the order of the migration is also known.
The other advantage with Laravel Migration is its ease of usage. So if someone else takes over the reins of development, it will not be a tough affair at all. All that needs to be done is to execute all the migrations.
Laravel comes in with an in built Command Line Interface or CLI. The CLI gives you access to all the command lines related to migrations.
To create a new migration file and let’s call it “man” all we need to do is run the following command:
$ php artisan make:migrationcreate_man_table --create=man
One of the advantages of working the Laravel framework is the Eloquent ORM. This works like magic and speeds up developmental work manifold. As suggested previously, the command lines are simple and the syntax, easy to remember. Another advantage which works in conjunction is the flexibility it provides while programming and extendable too.
Advantages and Disadvantages of Laravel Migration
Given below are the advantages and disadvantages:
Advantages
- It is a new framework.
- Web applications can be built on it quickly.
- It is easy to learn.
- The documentation is easily available.
- Fulfils almost every criterion demanded by modern web application build up.
Disadvantages
- Since Laravel is still new compared to the other frameworks, we can expect a few bugs present.
- Web Applications based on Legacy frameworks is not easily transferable to the Laravel framework, and a bit of work is required in order to do so.
- Quite few organizations prefer to stick to their legacy systems instead of re-inventing the wheel.
Conclusion
In conclusion, it would be unfair for me to suggest that Laravel Framework may probably be the most simplistic and yet powerful framework available currently, since every other framework does have its own advantages. However, it does come close to being perfect.
Recommended Articles
We hope that this EDUCBA information on “Laravel Migration” was beneficial to you. You can view EDUCBA’s recommended articles for more information.