Managing Your Database With Laravel 5 Migrations

This is a free resource from my online course, From Idea To Launch, where I teach you how to build a full Laravel web application, step by step, at beginner's speed. Learn more →

You can manage your Laravel 5 application’s entire database structure using migrations.

Migrations are PHP files that you create to build, and modify, your application’s database, over time.

Benefits of Using Migrations

With migrations, you no longer have to manually manage updates to your database.

It’s super easy to make updates

Need to make a database update? With migrations, you just:

  1. Create a migration file, using a simple command line command. (I’ll show you how in just a minute.)
  2. Execute that migration, using another simple command line command.

And when you create a new migration, Laravel timestamps them, so it knows in which order each update to your database was made.

And this organization leads to the another benefit…

Someone else can get up and running with a copy of your application’s database in no time

Since the database migration files are included within your application’s codebase, if you pass off your application to someone else, they can recreate your application’s entire database just by executing all of the migrations.

Easy as that.

Laravel also automatically keeps track of all of the migrations you run, which leads to the another benefit…

You can rollback migrations to “undo” them

Say you make an update to your database — maybe you add a new column to a table — and, soon after that, you decide: “Actually, I don’t really need this column.”

Since Laravel keeps track of your migrations, if you decide you want to “undo” one, you can run what’s called a migration rollback, to reverse the previous migration.

Again, all that takes is one simple command line command.

Let’s see how all this works in practice.

Creating a New Database Migration

Laravel’s built-in Artisan Command Line Interface (CLI) provides you with access to all of the migration-related command line commands you need.

To generate a new migration file to, say, create a new dogs database table, from within your application’s main directory, just run:


$ php artisan make:migration create_dogs_table --create=dogs

Which will generate a new migration file in the database/migrations folder named [timestamp]_create_dogs_table.php with the following code:


<?php

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

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

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

Note that every migration file contains a class with two main functions:

  • up — to designate the updates to be made to the database
  • down — to designate how to reverse the actions in up

Note that, in this case, up contains a Schema::create function, used to create the dogs table.

Laravel’s Schema builder provides you with the functions to make any possible database update you can imagine.

Note that it’s the down function that makes rollbacks possible.

In this case, the reverse of creating a table is, well … dropping it, hence the Schema::drop call, which would delete the dogs table.

And since we supplied that --create option when generating the migration, Laravel automatically supplied the boilerplate code for creating a new table.

To complete the migration, you would just need to define any additional columns within the Schema::create function. (See Laravel’s Schema builder documentation for defining all of the different column types.)

Running the migration

And then to run the migration and create the new dogs table, you’d simply execute the following command in the command line:


$ php artisan migrate

Rollback a migration

Performing a migration rollback is just as easy. To rollback the previous migration, just execute the following command in the command line:


$ php artisan migrate:rollback

In this case, it would delete the dogs table.