How to Create a Model with Migration Script in Laravel

Posted on

Introduction

This is an article where it has a connection with the previous article with the title of ‘How to Create a Model in Laravel’. Actually, that article which exist in this link is focusing on how to create a model only. But in this context, the article is not only focusing to create a model but also its associated migration script file. It is important to have a table from its associated model which in this case, it is the role of the migration script. In the end, the main goal is to have not only a model but also its associated table in the database using the migration script.

Create a Model with Migration Script in Laravel

This is the part where the explanation for creating a model with its associated migration script exist. In order to create it, the following are the steps to do it :

  1. It is obvious that the first step is to have a Laravel-based project or application. Read the article with the title of ” in this link. It is an article showing how to create a Laravel-based project or application.

  2. After that, run the command line interface.

  3. Following after, just access the path of the Laravel-based project or application. In this case, it is following example in the previous article where it exist in ‘C:\programming\php\laravel\crud’.

  4. In the root path of Laravel-based project or application, just execute the following command pattern in the command line interface :

    php artisan make:model model_name -m

    There is a slight difference on the above command pattern. There is an additional parameter ‘-m’ at the end of the command pattern. Without the additional parameter ‘-m’, it will only create a file representing model like the article in this link. But having an additional parameter ‘-m’ will add another file. There will be a migration script file generated by running the command with the above pattern.

  5. Using the above command pattern, below is the execution of the command in the actual one :

    C:\programming\php\laravel\crud> php artisan make:model Category -m
    Model created successfully.
    Created Migration: 2021_08_03_124211_create_categories_table
    C:\programming\php\laravel\crud>
    
  6. Soon after, try to to check the Laravel-based project or application. First of all, check the file representing the model. It exist in the folder with the name of app\Models. So, it will available as in the following output :
    C:\programming\php\laravel\crud\app\Models>dir
    Volume in drive C is Windows
    Volume Serial Number is E003-3593
    
    Directory of C:\programming\php\laravel\crud\app\Models>
    
    08/06/2021 07:38 AM <DIR> .
    08/06/2021 07:38 AM <DIR> ..
    08/04/2021 05:49 AM 959 Category.php
    07/13/2021 09:12 PM 832 User.php
    7 File(s) 3,189 bytes
    2 Dir(s) 4,223,680,512 bytes free
    
    C:\programming\php\laravel\crud\app\Models
    

    Another one is to check the migration script file. It exist in the folder of ‘database\migrations’ from the root location of the Laravel-based project or application. In that case, the exact location is in ‘C:\programming\php\laravel\crud\databas\migrations’. Just check it as follows :

    C:\programming\php\laravel\crud\database\migrations>dir
    Volume in drive C is Windows
    Volume Serial Number is E003-3593
    
    Directory of C:\programming\php\laravel\crud\database\migrations>
    
    08/06/2021 07:38 AM <DIR> .
    08/06/2021 07:38 AM <DIR> ..
    07/13/2021 09:12 PM 798 2014_10_12_000000_create_users_table.php
    07/13/2021 09:12 PM 683 2014_10_12_100000_create_password_resets_table.php
    07/13/2021 09:12 PM 820 2019_08_19_000000_create_failed_jobs_table.php
    08/03/2021 07:56 PM 679 2021_08_03_124211_create_categories_table.php
    08/06/2021 07:38 AM 574 2021_08_06_003809_create_tests_table.php
    8 File(s) 5,729 bytes
    2 Dir(s) 4,238,307,328 bytes free
    
    C:\programming\php\laravel\crud\database\migrations>
    

    As a conclusion, the process for creating a model with its associated migration script file is a success.

Leave a Reply