PHP Laravel Framework: Migration Tutorial

Following series of Laravel tutorial are covered in order to create a Web Application in PHP. Below are the links of tutorials in descending orders we have covered so far in order to setup Laravel Project:

  1. How to Use Authentication in Laravel 5.7?
  2. MySQL Server 8.0.15(Community Edition) Easy Installation in Windows 10
  3. Laravel5.7 CRUD Operations Examples from Scratch.
  4. Laravel5.7 : MySQL Setup and Initial Migration. [Solved] “Illuminate\Database\QueryException : could not find driver (MySQL)”
  5. How To Install Laravel Framework in Ubuntu?Laravel5.7, PHP7.1.26

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 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.

Following are the list of issues encountered during the installation of projects along with their solutions. This might save you time:

  1. PHP Internal Server Error 500. How to Generate Key in Laravel PHP?
  2. require(vendor/autoload.php):failed to open stream in Laravel5.7 PHP
  3. [ext-mbstring, ex-dom] How to Install missing PHP Packages/Extensions in Ubuntu?
  4. Messed Up Your .bashrc file? How To Fix your Linux Desktop?[Fix]
%d bloggers like this: