A migration allows you to create and update a database schema. In other words, you can create tables, columns in these tables, delete them, create indexes ... Everything related to the maintenance of your tables can be taken care of by this tool. You thus have a follow-up of your modifications.


BDD Configuration

You must first have a database. Laravel allows you to manage MySQL, Postgres, SQLite and SQL Server databases.

You must indicate where your database is located, its name, user name, password in the .env configuration file:


DB_CONNECTION=mysql

DB_HOST=127.0.0.1

DB_PORT=3306

DB_DATABASE=homestead

DB_USERNAME=homestead

DB_PASSWORD=secret

You will obviously have to modify these values according to your development context and above all define the name of the database, the name of the user and the password.

Artisan Migration Command

Laravel comes with set of migration related commands that can be run from Artisan command line interface:

  • fresh : deletes all tables and relaunches migration (command appeared with version 5.5)

  • install : creates and informs the reference table of migrations

  • refresh : reset and restart migrations

  • rollback : cancel the last migration

  • status : provides information on migrations


Creating a New Database Migration

Step 1:   In laravel, before creating any migration first thing is to run the following command to create migrations table.

php artisan migrate:install

As command executed successfully, you will notice a new table “migrations” is created in database.

Step 2:   Run following command to generate new migration file that will create a new table “posts” in database.

php artisan make:migration create_posts_table --create=posts

Once the above command executed you will see new migration file is created in the database/migrations directory named as [timestamp]_create_posts_table.php

Structure of a Migration

Let’s open the newly generated migration file, to take a look over the migration structure

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('posts');
    }
}

Note that every migrations we create extends the Migration class, and must contains two methods “up” and “down”,  here –

up() – The up () method includes the set of operation that is executed when the migration is run.

down() – The down () method includes the set of operations that is executed when you rollback migration , it usually  reverse the operations performed by the up () method.

In the example, up () method contains a Schema::create function, used to create the “posts” table.

In both of the above methods we can define set operations to create and modify database table using the Laravel schema builder.Laravel’s schema builder provides you the set functions to make any possible database update that you wish to apply.

For more details see the Laravel documentation.