Introducing Migrations for beginners

If you’re creating a database structure.laravel brings you a way to do that, it’s called Laravel Migrations. Basically, Migrations are PHP scripts that you can use to build or modify your database. Migrations keep a record of what you do.
Let’s create migrations to understand more clearly.

Creating Migrations

In order to create a migration, we will need to use PHP Artisan. First, we open Terminal (or Command Prompt). After that, we navigate to our project folder:
1 cd /var/www/html/todo/code
Note: I put my project on var/www/html/todo,and my app is called todo. Your path and your project name may be different.
We will create our users table use migrations When we’re in our project folder, executing this command:
1 php artisan migrate:make create_users_table
To create a migration, you may use the migrate:make command on the Artisan CLI
The --table and --create options may also be used to indicate the name of the table, and whether the migration will be creating a new table:

php artisan migrate:make add_votes_to_user_table --table=users

php artisan migrate:make create_users_table --create=users
When we execute Artisan migrate:make command, Laravel creates a new migration template witha timestamp for us. The template is located in app/database/migrations. create_users_table is the nameof the template, you can name it whatever you like. For example, you can find in the migrations folder a file look like this:

1 2014_01_13_110107_create_users_table.php

*Please note that the timestamp (20140113_110107) could be different. *
Open it with a text editor, we should see something like:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsers extends Migration {
/**
* Run the migrations.
*
* @return void
*
public function up()
{
//
}
 /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
There are 2 methods, up() and down(). We will write our schema building code inside those methods.When we need to do something, we write it in up() method. When we need to undo something, we write it in down() method. Easy?
Let’s try to create our users table by filling the up() method:
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('title');
$table->text('body');
$table->integer('user_id');
$table->boolean('done');
$table->timestamps();
});
}

Remember, when we create something in a up() method, always make sure that we remove it in adown method().

If we need to create a migration faster, we can use:
 1 php artisan migrate:make create_users --create --table=users
By adding –create –table=users, migrations will automatically create a template with a users table for us. The template looks like
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsers extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tasks');
}
}
We’ve just created a migration template. We must execute it to create our users table.
To run our migration, it’s very easy:
 1 php artisan migrate
Note: make sure that you’ve deleted the users table before running migrations.
We’ve just created our users table again using migrations. When you use php artisan migrate, Laravel will look for all migration templates, and execute all up() methods, if those methods are not executed yet.
If everything is ok, check your database using phpMyAdmin or any database tool you like, we should see:
Our database structure in phpMyAdmin
Laravel checks and creates a migrations table for us, if the table doesn’t exist. Migrations table is used to keep track of all the migrations that have already run for our application. It’s a part of migrations system. If you don’t like its name, you can change it in app/config/database.php:
1 'migrations'=>'migrations',
Change ⇒‘migrations’ to whatever you like.
Here is a little trick, if we want to see the intended SQL result of a migration command, we can use:
1 php artisan migrate --pretend
Basically, adding –pretend will show us the SQL queries, that would be executed:
Using –pretend
 Rolling Back Migrations
we’ve executed the up() method and created our users table. If we want to drop the table, we
can use migrate:rollback command:
1 php artisan migrate:rollback
We’ve just dropped our table by running a migration command. The command executed our down() method.
A little note, migrate:rollback command rolls back only the migrations that were ran that last time we used migrate command. If we have many migrations, and we need to roll back all of them, we use migrate:reset command:
1 php artisan migrate:reset
Database seeding
Laravel also includes a simple way to seed your database with test data using seed classes. All seed classes are stored in app/database/seeds

Example for Database Seed Class

class DatabaseSeeder extends Seeder {

    public function run()
    {
        $this->call('UserTableSeeder');

        $this->command->info('User table seeded!');
    }

}

class UserTableSeeder extends Seeder {

    public function run()
    {
        DB::table('users')->delete();

        User::create(array('email' => 'foo@bar.com'));
    }

}
To seed your database, you may use the db:seed command on the Artisan CLI:
1. php artisan db:seed

which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:

2.php artisan db:seed --class=UserTableSeeder
which will also rollback and re-run all of your migrations:
3.php artisan migrate:refresh --seed
At this time, you can be able to create a database structure using migrations. You can try to practice by creating some tables and then roll them back.
The ability to create tables and drop/rollback them by using only a single line of code.