Laravel is powerful framework out of the box that is capable to solve any problem you have throughout all kind of activities of the business unit that you manage or barely you can do alone, I mean, Laravel will do anything for you with no efforts, easing those gaps found in the PHP language specially, making more clean its code and having an enhanced experience for the developer.
Migrations are one of the things where you as a developer even a entrepreneur, you can make great apps more flexible and adaptive to any mode you wish running your data, since syntax of laravel became more readable and easy to understand in order to design all your datamodel with no thinking about on what engine should you use, instead of that you need to think in a conceptual-way setting out whole a bunch of piece of your idea breaking down into the different component that will be made-up your app.
So, if you are a newbie in this, do not worry, maybe would be a little bit tricky for you, but, you’re going to catch up as soon as nemo was found (insert a ba-dum-tss meme here).
First at all, we must see that in a traditional environment if we need to do a change in the database, we must rewrite many things because we do not count with a version control that allow us to do an accurate modification which this requires an operation through an advanced interface trying to read a language that in the most cases become hard to read and maintain which is based on scripting in this case is SQL, causing us to lose focus on what we need to do really in the app.
That’s why exist this concept of migrations which they are files located under the directory
databases/migrations of our project, I assume you have a basic idea on how get a fresh installation of Laravel working, thus by default there are two migrations that you can run in anytime with an special command which I’ll talk about it later.
These two migrations what I’m talking about is create_usertable and create_password_resets_table. However, to create new migrations files in Laravel, you can do that by following:
php artisan make:migration <migration_name>
this command allow to create a clean file in order to we can write our migration, or just you can type the following parameter:
php artisan make:migration <migration_name_here> --create=tablename
this one is able to create a basic template to get started to work, which it’ll output below result:
Created Migration: 2017_01_23_054801_migration_name_here
Consider write something congruent where it says migration_name_here, since it creates for us a file with the following content like this:
Now we have a look at two important things before you write any code in there; first you need to catch up that there are two function where you need to put the necessary code in order to do the correct operation into the database. The function up() is used to push a schema normally with the definition of the table you need to create or alter, considering to put all the thing together within the closure which is sent to the class Schema through the method create and the function down() should be the opposite operation, in this case is drop or delete the creation of the table. If you need more information you can check out the well-documented guide to add some columns to your table. These definition for the table you can do them with the object Blueprint that is passed as an argument to the closure in the create method.
For instance, this will output something like this, if we see in SQL language.
create table users (id int auto_increment, remember_token text null);
While each function do the respectively task, we can use them through the command line with artisan:
php artisan migrate
If it is the first time you run the migration, it will create a table named as migrations which comes a version control that avoid to read same files has already run if the command above is run again.
But, if we need to delete migrations? Easy, this one could be resolved just typing
php artisan migrate:rollback which it is going to delete last migration you had run.
And if you type
php artisan migrate:reset this one it is going to delete all migrations you had created.
As an important note:
Summarizing, consider the order that you’re going to create migrations files, because this one is going to be how should be run them, both to migrate and to rollback/reset.