Spread the love

Migrations are used to manage the version control among the developers so that they can sync database schema to each other. Laravel itself provides database migration capabilities to manage database schema updates like create table, alter, modify, drop etc.

Main advantage of migrations are to maintain the same schema across the team mates and you can update database schema without accessing the ssh or MySQL console.

In this article i will show you to create a simple table with auto increment column, title column and date columns.

For this purpose laravel provides an artisan command make:migration to generate the migrations.

Let’s understand the create migration in laravel step by step

Step 1 : Generate migration file

To generate the migration file we will use the laravel artisan command so open the terminal in project and run below command

php artisan make:migration create_movies_table

Above command will create a migration file in folder database/migrations

Output: 
Created Migration: 2022_02_15_164343_create_movies_table

Step 2 : Open generated migration file and update

In the last step we created a migration file using the artisan command and now we wanted to add some more columns to schema of movies table.

so let’s open the file and start editing

<?php

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

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

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

In the file there is a class named as CreateMoviesTable and it extends Migration class. CreateMoviesTable contains two methods one is up and other one is down.

Up is used to update the database scheme and down method is used to rollback the changes of this migration. As you can see we are creating table movies in up method and dropping the table in down method.

let’s add some more fields to our table before updating the database.

 public function up()
    {
        Schema::create('movies', function (Blueprint $table) {
            $table->id();
            $table->string("title");
            $table->text("description");
            $table->timestamps();
        });
    }

Step 3 : Run Migration

In this step we will execute our migration in database using below command

php artisan migrate

This will create table in database and the output

Output: 
Migrating: 2022_02_15_164343_create_movies_table
Migrated:  2022_02_15_164343_create_movies_table (33.58ms)

Screenshot:

Laravel migration in database

How to rollback a migration in laravel ?

Once you created a migration and executed the php artisan migrate you want to revert this migration, so in this case we also write our logic in down method to rollback the our migration.

We can easily rollback our migration using below command

php artisan migrate:rollback

This will rollback the last migration. If you want to rollback last 5 or 6 migrations then you can use --step option

php artisan migrate:rollback --step=5

To rollback all migrations

php artisan migrate:reset

Also Read : How to run raw SQL query in migration Laravel ?

Leave a Reply