PHP Laravel Framework: Migration Tutorial

Migration means defining the database table schema in your code and pushing the change to your database. This blog covers the following things:

  1. Creating a Migration table
  2. Migrating a table to the database
  3. Rollback the applied migrations

Creating a Migration Table

During the fresh installation of the Laravel App as mentioned in the earlier blog(Laravel Installation), Laravel automatically creates two migration tables create_users_table.php and create_password_resets_table.php.  So once you type php artisan migrate, it will create two tables in your database.

In order to create a new migration file you need to do the following:

php artisan make:migration create_test1_table --create=test1

This will create a migration file with a timestamp appended in its prefix. This is where we define the table schema.

After you will create this migration file take a look at the code. There are two functions in the migration file: function up() and function down()

/**
  * Run the migrations.
  * @return void
  */
public function up()
    {
        Schema::create('test1', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('test1');
    }

The up function is where your migration is applied in the database. While the down function is where your migration rollback action is performed.

Migrating a table to the database

php artisan migrate migrates the initial tables created during the application setup. Now that you have created a test1 table with above php command. How are you going to migrate it? If you will type php artisan migrate it will show you results like:
table ‘users’ already exists. This migrate command will not include newly included tables. To migrate the new tables you need to do the following:

php artisan migrate --path=/database/migrations/2019_05_07_115322_create_test1_table.php

We provided a direct path in the migration command and now this will migrate the test1 table. you will see the log in console or you can also check your database using database query to display tables in your database.

Rollback the applied migrations

In the above examples, a migration file for defining a schema of table test1 was created. After that next step to migrate a particular file was also covered. Now in the next phase, we will first create a few migrations. In order to do so, create some more migration tables.

  1. Remember to check the PHP filename while providing the specific path.
  2. First, check the timestamp appended with the migration file and then perform a migration

Create about 5 such migration file and also perform the migration. First, on your database check the number of tables available. Now after performing the migration in order to perform the rollback first type the following:

php artisan mirgate:rollback --step=3

After doing this again check the database. This will remove three tables which you have created earlier. In other terms, it will remove three migrations which you have performed earlier.

Now simply type the following:

php artisan mirgate:rollback

This will remove all of the migrations that you have applied. The first migration which you performed i.e the ones which created these two tables: users and migrations,  won’t be rolled back. Besides that, your entire migration will be erased.