Introduction to Laravel Migrations

When developing web applications, managing database structure is an important task. Laravel migrations are like version control for your database. They allow you to define the structure of your database using PHP code, and then run the migrations to apply the changes to your database. This makes it easy to modify, share, and roll back database changes.

In this blog, we will walk through how to create migrations, run them, and even rollback changes with some simple examples!

What Are Laravel Migrations?

Laravel migrations provide a way to manage your database schema, like creating tables, adding columns, and modifying them. They are part of Laravel’s powerful ORM (Object-Relational Mapping) system, Eloquent. With migrations, you can easily make changes to your database and share those changes with other developers.

Step 1 – Creating a Migration

To create a migration in Laravel, you can use Artisan, which is Laravel’s command-line tool. Open up your terminal or command prompt and run the following command:

php artisan make:migration create_posts_table

This command creates a new migration file in the database/migrations directory. The name of the migration (create_posts_table) can be anything descriptive, and Laravel will automatically generate a timestamp in the filename to track the order of migrations.

Step 2 – Inside the Migration File

After running the above command, Laravel generates a file with a structure like this:

<?php

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

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

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

up() method: This method defines the changes to the database schema. In this case, it creates a posts table with an auto-incrementing id, a title, a body, and timestamp columns (created_at, updated_at).

down() method: This method reverses the changes in case you want to roll back the migration. In this case, it deletes the posts table.

Step 3 – Running Migrations

To apply the migration and create the table, you can run:

php artisan migrate

This will execute all of your pending migrations and apply them to the database.

Rolling Back Migrations

If you want to undo the changes made by a migration (for example, if you made a mistake or need to modify the schema), you can run:

php artisan migrate:rollback

This command runs the down() method in your migration, undoing the changes to your database.

Modifying Existing Migrations

What if you want to modify an existing table or add new columns? You can create another migration to handle that. For example, if you want to add an author column to the posts table, you can run:

php artisan make:migration add_author_to_posts_table --table=posts

This creates a new migration file. Inside it, you would define the changes:


public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->string('author')->nullable();
});
}

public function down()
{
Schema::table('posts', function (Blueprint $table) {
$table->dropColumn('author');
});
}

Now, when you run php artisan migrate, it will add the author column to the posts table.