Laravel Logo

Let’s start with my favorite feature so far: database migrations. I’m going to start with this command because I think it’s one of the more critical components and because the next article will involve working with them. :-)

Why It Exists

Database migrations allow you to define changes to your database so you can easily apply the changes everywhere you maintain a copy of the database for the application. After working in a product with no database versioning I’m a huge fan of any system that does and it’s great that Laravel supports this out of the box.

When Should You Use It

Any time you’re creating a new table or editing the structure of an existing table.

Making a New Table

In order to make a new table you can run the command with --create parameter:

$php artisan make:migration --create=new_table_name name_of_migration
Created Migration: 2019_09_13_012814_name_of_migration

And that produces the following PHP class:

<?php

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

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

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

You can also be a little lazy and prefix your migration name with “create_” and it will infer the table name from the migration name.

php artisan make:migration create_roles
Created Migration: 2019_09_12_013314_create_roles

This produces the following up function:

public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->timestamps();
    });
}

Altering an Existing Table

In order to alter an existing table you can run the command with --table parameter:

/var/www$ php artisan make:migration --table=roles add_name_to_roles
Created Migration: 2019_09_12_013847_add_name_to_roles

Which generates the following PHP class.

<?php

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

class AddNameToRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('roles', function (Blueprint $table) {
            //
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('roles', function (Blueprint $table) {
            //
        });
    }
}

But we can also be lazy about this process. If we look into Illuminate\Database\Console\Migrations\TableGuesser we can find the following regular expressions which determine migration names that will automatically determine the --table argument.

const CHANGE_PATTERNS = [
    '/_(to|from|in)_(\w+)_table$/',
    '/_(to|from|in)_(\w+)$/',
];

So can run the following command and have it work without the --table argument.

/var/www$ php artisan make:migration add_name_to_roles
Created Migration: 2019_09_12_013847_add_name_to_roles

I’m going to skip the PHP output for this because it’s identical to the output above. :-)

Hopefully this has been as helpful to you as it has to me and check back soon for more artisan commands.