How do Laravel migrations work? How do Laravel migrations work? laravel laravel

How do Laravel migrations work?


Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.

With migrations you don't need to create table in phpMyAdmin, you can do it in Laravel. Here is an example to create a user table:

class CreateUsersTable extends Migration {    /**     * Run the migrations.     *     * @return void     */    public function up()    {        Schema::create('users', function(Blueprint $table)        {            $table->increments('id'); // autoincrement id field            $table->string('name');   // string field            $table->string('lastname');            $table->string('title');            $table->string('email')->unique();   // unique string field            $table->string('password', 60);      // string field with max 60 characters            $table->boolean('Status')->default(0); // string field with default value 0            $table->timestamps();        });    }    /**     * Reverse the migrations.     *     * @return void     */    public function down()    {        Schema::drop('users');    }}

I this code we create table with fields like "name", "lastname"... we said in our Laravel code they are string type when migration is done we have complete table in databese with this fields.

Run a migration to create table

To create a migration, you may use the make:migration command on the Artisan CLI (artisan command line interface):

php artisan make:migration create_users_table

or

php artisan make:migration create_users_table --create=users

Run a migration to alter table

When you need to do some changes in database table example: add field vote to user table you can do like this in your Laravel code without touching SQL code

php artisan make:migration add_votes_to_users_table --table=users

Rollback the last migration operation

If you make mistake and did something wrong you can always rollback to return database in previous state.

php artisan migrate:rollback

Rollback all migrations

php artisan migrate:reset

Rollback all migrations and run them all again

php artisan migrate:refreshphp artisan migrate:refresh --seed

One of best advantage of migrations are creating database without touching SQL code. You can make whole database with relationship in PHP code then migrate it into MySQL, PL/SQL, MSSQL or any other database.

Also I recommend the free Laravel 5 fundamental series, in episode 7 you can hear more about migrations.


A simple explanation of migrations:

They're a versioning system for your database scheme.

Imagine you're setting up a new application. The first thing you do is create a table (call it mytable) with a couple of columns. This will be your first migration. You run the migration (php artisan migrate) when you start working on your application and voila! You have a new table in your database.

Some time later, you decide that you need a new column in your table. You create a migration (php artisan make:migration in Laravel 5) and a new migration file is created for you. You update the code in that migration, run php artisan migrate again, and you have a new column in your table.

Ok, that's the basic idea behind migrations. But there's more...

Suppose, later on, you realize that you messed up when you created that last migration. You've already run it, so your database has changed. How to you fix it? Well, if you wrote your migration correctly and implemented the down method, you can just do php artisan migrate:rollback and it will rollback the migration.

How does Laravel do this? By keeping track of your migrations in a special database table. The php artisan migrate:install command will set things up for you so Laravel can manage these migrations.

Over time, as your application grows, you will add more and more migrations. Laravel gives you a way to step forward and back through your migrations as needed to ensure your database is at whatever state you need it to be as you're working.

Check out the list of artisan commands with php artisan. You can also request help on a particular command with php artisan help <command>.


Imagine you are working on a project with other developers. As developers are adding more and more features, they are also required to add more and more tables, columns, remove columns, change column data types, etc...

This can very quickly and easily get out of control. If you missed some SQL from one developer, another developer's SQL may not work correctly. It also potentially becomes a huge time waster trying to sort through a bunch of sql files trying to figure out which ones you've missed. It's only a matter of time till everyone on the development team is working with different databases or god forbid, someone breaks the production database. With migrations, you just need to run php artisan migrate in the command line and all changes to the database will be taken care of for you.

This is basically why migrations are useful I'm not going to go into the basics of how they work because Kryten has a pretty good write-up here already.