How to Create Table in MySQL Database using Laravel Migration

Posted on

Introduction

This is an article about how to create a table in a database. Specifically, the table in a database running in MySQL database server. But in this case, the process for creating the table inside a database running in MySQL database is using Laravel migration tool. Instead of using a specific query execution, it will execute the Laravel migration script. It is very useful for collaborating with many people working the same project. Initiating database with the modification is going to have a standard result since it is using the same script with Laravel migration tool.

Create Table in MySQL Database using Laravel Migration

So, how to create a table in a database running using MySQL database without having to involve any query execution ?. The following are those steps to achieve the purpose :

  1. First of all, create the Laravel-based project. Read the article with the title of ‘How to Create a Laravel Project using Composer in Microsoft Windows’ in this link for a reference and sample.

  2. Following after, create the necessary database with the associated username and password for accessing it.

  3. Next, the most important thing is editing the file of the database connection configuration of the Laravel-based project. It exist in the root or top level of the Laravel-based project. For an example, in this case it is ‘C:\programming\laravel\crud’. The file name is ‘.env’ so, it will exist in the path of ‘C:\programming\laravel\crud\.env’. just access the root location of the Laravel-based project. The most important part is in the following snippet code configuration :

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=
    DB_USERNAME=root
    DB_PASSWORD=
    

    Adjust the above configuration code according to the database exist in the operating system. In this case, the database name is ‘db_app_laravel’. With the username of ‘root’ and the password of ‘password’, the snippet code configuration will exist as follows :

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=db_app_laravel
    DB_USERNAME=root
    DB_PASSWORD=password
    
  4. After that, just execute the following command pattern to create a migration script file in the command line :

    php artisan make:migration migration_script_file_name

    Using the above command pattern, in order to create a migration script file with the aim of creating a table with the name of ’employee’, the following is the execution of it :

    C:\programming\php\laravel\crud> php artisan make:migration create_employee_table                                                                         
    Created Migration: 2021_08_01_093359_create_employee_table                                                 
    C:\programming\php\laravel\crud>
    

    Moreover, check the article with the title of ‘How to Create Migration Script in Laravel’ in this link to find more information.

  5. Edit the migration script file. It is normally by default exist in the database\migrations folder. So, if in this case the root location of the Laravel-based project is in ‘C:\programming\php\laravel\crud’, the script migration file exist in C:\programming\php\laravel\crud\database\migrations folder.

    As in the above output command execution, the file name is ‘2021_08_01_093359_create_employee_table’. So, edit the file to add another column or additional field. Actually, the following is the snippet code content for the table definition by default :

    public function up()
    {
            Schema::create('employee', function (Blueprint $table) {
                $table->id();
                $table->timestamps();
            });
    }
    
  6. In order to create an additional column or field, just edit the above migration script by adding some more lines. In this context, editing the migration script is aiming to add two more columns. The first column is a column with the name of ‘name’ with the string type. The second column is a column with the name of ‘birthdate’ with the date type :

    public function up()
    {
            Schema::create('employee', function (Blueprint $table) {
                $table->id();
                $table->string('name');
                $table->date('birthdate');
                $table->timestamps();
            });
    }
    
  7. After editing the migration script file, just execute the following command pattern :

    php artisan migrate

    Before executing it, check the current database condition. Actually, it is useful to check whether the table exist or not. For an example, also create the database too. Below are the execution queries :

    mysql> create database db_app_laravel;
    Query OK, 1 row affected (5.68 sec)
    mysql> use db_app_laravel;
    Database changed
    mysql> show tables;
    Empty set (0.00 sec)
    mysql>
    

    After that, just execute the above command pattern exist as follows :

    C:\programming\php\laravel\crud> php artisan migrate         
    Migration table created successfully.
    Migrating: 2014_10_12_000000_create_users_table
    Migrated:  2014_10_12_000000_create_users_table (1,970.20ms)
    Migrating: 2014_10_12_100000_create_password_resets_table
    Migrated:  2014_10_12_100000_create_password_resets_table (1,501.91ms)
    Migrating: 2019_08_19_000000_create_failed_jobs_table
    Migrated:  2019_08_19_000000_create_failed_jobs_table (1,697.04ms)
    Migrating: 2021_08_01_093359_create_employee_table
    Migrated:  2021_08_01_093359_create_employee_table (751.52ms)
    C:\programming\php\laravel\crud>
    
  8. Finally, check the existence of new created table after executing the above migration script :
    mysql> use db_app_laravel;
    Database changed
    mysql> show tables;
    +------------------------------------+
    | Tables_in_db_app_laravel           |
    +------------------------------------+
    | employee                           |
    | failed_jobs                        |
    | migrations                         |
    | password_resets                    |
    | users                              |
    +------------------------------------+
    5 rows in set (0.00 sec)
    mysql>
    

    At last, seeing the output of the above command concludes the successful execution of the migration script to create a new table using Laravel migration tool.

Leave a Reply