How to Create a Database Seeder in Laravel

Posted on

Introduction

This is an article which is focusing on how to create a database seeder in a Laraval-based project or application. Off course, there must be a Laravel-based program first in order to do that. Read the article with the title of ‘How to Create a Laravel Project using Composer in Microsoft Windows’ in this link to find out about how to do it.

Create a Database Seeder in Laravel

In order to create a database seeder in Laravel-based project or application, the following are the steps to achieve it :

  1. First of all, having a Laravel-based project or application is a must.
  2. Next, execute or run a command line interface to perform the command. It is obvious since creating a database seeder requires running or executing a command.
  3. The command for creating a database seeder which is actually in the form of a file exist in the following pattern :
    php artisan make:seeder database_seeder_file_name
  4. Before executing the above command pattern, the following is the actual environment condition for actual execution. Actually, the Laravel-based project or application is in ‘C:\programming\php\laravel\crud’. So, the execution of the above command will be in ‘C:\programming\php\laravel\crud’.

  5. The following is the actual execution of the command pattern above :

    C:\programming\php\laravel\crud> php artisan make:seeder CategorySeeder      
    Seeder created successfully.
    C:\programming\php\laravel\crud>
    

    In the command execution above, there is a hidden convention or rule in the naming of the database seeder file. It actually end with ‘Seeder’. It also starts with the table name where the target or data initialization exist. In this case, the name of the target of the table is ‘categories’. So, the name of the database seeder file will be ‘CategorySeeder’. After executing the above command, just make sure that the file exist.

  6. This is the last step for checking the existance of the file where the location of the Database Seeder file will be in ‘C:\programming\php\laravel\crud\database\seeder’.

    C:\programming\php\laravel\crud\database\seeders>dir
    Volume in drive C is Windows
    Volume Serial Number is E003-3593
    
    Directory of C:\programming\php\laravel\crud\database\seeders
    
    07/13/2021 09:12 PM <DIR> .
    07/13/2021 09:12 PM <DIR> ..
    08/01/2021 09:12 PM 187 CategorySeeder.php
    07/13/2021 09:12 PM 282 DatabaseSeeder.php
    1 File(s) 282 bytes
    2 Dir(s) 2,174,881,792 bytes free
    
    C:\programming\php\laravel\crud\database\seeders>

    The above output present the Database Seeder file exist as the execution of the above.

Leave a Reply