Generating Table in Laravel with PHP Artisan Migrate

Posted on

Programming Language : PHP 7.0

Framework : Laravel 5

Operating System : Ubuntu 16.04 (Xenial Xerus)

Generating table for the usage of Laravel project

The purpose of generating table to be used in Laravel project can be achieved by using the utility provided named ‘artisan’ is very easy. All you have to do is execute the following steps in the Laravel username directory folder as shown below :

  1. Create a migration script
php artisan make:migrations script_migration_name

The example is shown below :

username@hostname:/var/www/html/laravel-project# php artisan make:migration create_project_and_tasks_tables
Created Migration: 2016_07_07_130638_create_file_uploads_table
username@hostname:/var/www/html/laravel_project#

As shown in the above example, the root folder of the Laravel project is in :

/var/www/html/laravel-project.

We have to execute the above command exactly in that path. If you are not sure whether you are in the right path, you can check it with this command to print the currently working directory or you can simply execute the command.

The above command will generate a migration file which exists in folder

database/migrations

So, the full path will be in :

/var/www/html/laravel-project/database/migrations

Based on the output of the command displayed above, the migration file generated’s name is 2016_07_07_130638_create_file_uploads_table

  1. Edit the migration file.

Editing migration file can be done by using any kind of programs or utilities available. In this post, I am using vim command-line editor.

username@hostname:/var/www/html/laravel_project# cd database/migrations/
username@hostname:/var/www/html/laravel_project/database/migrations# ls -al
total 28
drwxrwxr-x 2 www-data www-data 4096 Jul  7 20:09 .
drwxrwxr-x 5 www-data www-data 4096 Apr 27 20:01 ..
-rw-rw-r-- 1 www-data www-data  699 Apr 27 20:01 2014_10_12_000000_create_users_table.php
-rw-rw-r-- 1 www-data www-data  633 Apr 27 20:01 2014_10_12_100000_create_password_resets_table.php
-rw-r--r-- 1 www-data www-data 1103 Jul  7 20:05 2016_07_07_125815_create_project_and_tasks_tables.php
-rw-r--r-- 1 www-data www-data  647 Jul  7 20:09 2016_07_07_130638_create_file_upload_table.php
-rw-rw-r-- 1 www-data www-data    1 Apr 27 20:01 .gitkeep
root@soulreaper:/var/www/html/thesis/database/migrations#

Below is the content of the migration script file which is opened to be edited by executing the following command :

<?php
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration;
class CreateFileUploadTable extends Migration 
{
     /**      * Run the migrations.
      *
      * @return void
      */
     public function up()
     {
         //
     }
     /**
      * Reverse the migrations.
      *
      * @return void
      */
     public function down()
     {
         //
     }
 }

To be able to specify a specific table with several fields, we have to define it in the migration script file so it can be executed properly to generate the correct table. The table which is going to be generated is a simple table used to contain uploaded file information. Below is the entry snippet code which is needed to be inserted :

Inserting snippet code in the up() function :

     /**
      * Run the migrations.
      *
      * @return void
      */
      public function up()
      {         
        //
        // The inserted snippet code is defined below :
        Schema::create('file_uploads', function(Blueprint $table)
        {
                $table->increments('id');
                $table->string('name')->default('');
                $table->timestamps();
        });
}

Inserting snippet code in the down() function :

/**
      * Reverse the migrations.
      *
      * @return void
      */
      public function down()
      {
            //
            // The inserted snippet code is defined below :
            Schema::drop('file_uploads');
      }
    1. Executing the migration process

After editing the migration script file, execute the following command in the root folder of the Laravel project :

username@hostname:/var/www/html/laravel_project/database/migrations#
username@hostname:/var/www/html/laravel_project# php artisan migrate
Migrated: 2016_07_07_130638_create_file_uploads_table
username@hostname:/var/www/html/laravel_project#
  1. Check the table

Soon after successfully executing the migration script file, we have to check the database which is used by the Laravel project whether the related table already been created.

Log in to MySQL console :

username@hostname:/var/www/html/laravel_project# mysql -uusername -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 54
Server version: 5.7.12-0ubuntu1.1 (Ubuntu)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

The moment after succeeding on login to MySQL console, activate the database which is going to be operated. The database itself off course is the database which is used by the Laravel project. We can find it usually in the .env file located in Laravel root directory. In this context, the database name is ‘laravel_project_db’. So, we type the following command pattern in MySQL console :

use database_name

As shown in MySQL console :

mysql> use laravel_project_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed

Based on the migration script file, there is one table which is specificed to be created, that table based on the snippet code is file_uploads as shown below :

Schema::create('file_uploads', function(Blueprint $table)
Execute ‘show tables’ in MySQL console to view all the available tables in the database’s Laravel project :
mysql> show tables;
+------------------------------+
| Tables_in_laravel_project_db |
+------------------------------+
| file                         |
| file_uploads                 |
| migrations                   |
| password_resets              |
| projects                     |
| tasks                        |
| users                        |
+------------------------------+
7 rows in set (0,00 sec)
mysql>

To be able to prove it further, we can execute the following command :

mysql> desc file_uploads;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255)     | NO   |     |         |                |
| created_at | timestamp        | YES  |     | NULL    |                |
| updated_at | timestamp        | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
4 rows in set (0,01 sec)
mysql>

The fields which have already been generated are :

  1. id
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment

Which is acquired from the following line ofsnippet code :

                $table->increments('id');
  1. name
| name       | varchar(255)     | NO   |     |         |              |

Which is acquired from the following line ofsnippet code :

                $table->string('name')->default('');
| created_at | timestamp        | YES  |     | NULL    |              |
| updated_at | timestamp        | YES  |     | NULL    |              |

Which is acquired from the following line ofsnippet code :

                $table->timestamps();

 

One thought on “Generating Table in Laravel with PHP Artisan Migrate

Leave a Reply