Table of Contents

Laravel Migrations

Laravel Migrations

A zombie developer exports and imports SQL files, whereas a Laravel developer uses migrations to import and export database schemas.

Exporting and importing SQL files are dead, anyone who performs these actions have zombie like tendencies.

There is a new way of doing data backup which is called Migrations.


Migrations

How are migrations helpful? Glad you asked...

Say you are working on a team and someone adds a new row to the database, they'll have to send you over an updated database schema which you will have to import and make sure there are no conflicts!

It can get really complicated having to manage multiple iterations of your database schema by passing files amongst team members!

Well, thanks to migrations, MySQL dumps will be a thing of the past and sharing your updated schemas with teammates will be super simple. By using migrations, you can create files in your app that store the database structure.

If you remember back in section 5 we created a zombies table that looked like the following:

zombies

Field Type Length
id INT 11
name VARCHAR 50
strength VARCHAR 20
health INT 3
created_at TIMESTAMP
updated_at TIMESTAMP

Usually to create a database table we would open up phpMyAdmin, Sequel Pro, or some other kind of sequel application and add each of these rows one at a time.

Alternatively if we are using migrations we can leverage our good 'ol friend artisan to help us with this.

Let's create our first migration using the artisan tool:

$ php artisan make:migration create_zombies_table

After running the following command, you will see a new file that has been created inside your project located in the database\migrations folder, and it probably looks something like:

XXXX_XX_XX_XXXXXX_create_zombie_table.php, the X's resemble a datetimestamp.

Let's open up our new zombie migration file and you should see something similar to the following:

<?php

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

class CreateZombiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {

    }
}

Notice that there are 2 methods available in our new migration class.

We have an up() method and a down() method. The up method is used to modify something in our database and the down method is used to reverse what we did in the up method.

Let's add some code to the up() method that looks like the following:

public function up()
{
    Schema::create('zombies', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('strength');
        $table->tinyInteger('health');
        $table->timestamps();
    });
}

Now, if we have an empty database and we were to run the following command:

$ php artisan migrate

All our migrations will then be run and we should see a few tables inside our database including the zombies table.

If we were to add the following to our down function:

public function down()
{
    Schema::drop('zombies');
}

And we run:

$ php artisan migrate:rollback

It will run our down() method and we will no longer see our zombies table in our database.

Hopefully, you can see the power of migrations. We could easily share a GitHub repo with another user and they could pull our code down and create a new database. Then they can run the migrations and they will have the most up-to-date database schema.

There are many other types of functionality you can run in your up and down methods, be sure to checkout the full documentation on migrations at Laravel's documentation to learn more.

Fantastic!

Migrations allow us to save and version our database schema in our project files! What about the actual data that gets inserted into our database? What if we had some data that we wanted to seed into our database schema. Simple enough we can do that by using Seeds. Let's learn more about these in the next section.