How to Execute a Database Seeder in Laravel

Posted on

Introduction

This article’s content is focusing about how to execute a database seeder. It is a file for seeding or initializing data in a database. It exist in a Laravel-based project or application. But by default it is not exist at the first time. There must be a process that is exist in order to be able to create or to generate the database seeder file first. In order to create or to generate the database, just read an article exist in this link. It is an article with the title of ‘How to Create a Database Seeder in Laravel’.

How to Execute a Database Seeder in Laravel

This part is showing about the steps for executing a database seeder in a Laravel-based project or application. The following are the steps for executing it :

  1. It is necessary to have a Laravel-based project or application. Read the article with the title of ‘How to Create a Laravel Project using Composer in Microsoft Windows’ in this link as a reference.

  2. Next step, it is also necessary to have a database seeder file. In order to create a new Database Seeder file, just read the article with the title of ‘How to Create a Database Seeder in Laravel’ in this link.

  3. Continue on the previous step, just run the command line interface to run the command for executing a database seeder.

  4. Access the location or the path of the Laravel-based project or application. In this case, it is in ‘C:\programming\language\php\laravel\crud\database\seeders’. So, the file for the database seeder if it is following the article of ‘How to Create a Database Seeder’ in this link, it will have the name of ‘CategorySeeder’. The following is the actual content of it :

    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>
    
  5. The following is the execution of the file database seeder for filling in the table of the database. The name of the table’s target in this case is ‘category’. Furthermore, the database name is ‘db_app_laravel’ in this example. Before executing the file, edit the file first with the following content :

    <?php
    namespace Database\Seeders;
    use Illuminate\Database\Seeder;
    class CategorySeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            //
        }
    }
    ?>
    

    Add the following lines as the data row for the table as an example :

           
    \App\Models\Category::insert([
    ['name' => 'Application', 'parent_id' => 0,],
    ['name' => 'Database', 'parent_id' => 0,],
    ['name' => 'Networking', 'parent_id' => 0,],
    ['name' => 'Server', 'parent_id' => 0,],
    ]);
    

    Overall, the content of the CategorySeeder will exist as follows :

    <?php
    namespace Database\Seeders;
    use Illuminate\Database\Seeder;
    class CategorySeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            //
            \App\Models\Category::insert([ 
            ['name' => 'Application', 'parent_id' => 0,], 
            ['name' => 'Database', 'parent_id' => 0,], 
            ['name' => 'Networking', 'parent_id' => 0,], 
            ['name' => 'Server', 'parent_id' => 0,], ]);
        }
    }
    ?>
    
  6. Before executing the Database Seeder file, just check the table’s content as follow :

    mysql> select * from categories;
    Empty set (0.18 sec)
    
    mysql>
    
  7. Since it is still empty without any row or data record, just execute the command for generating row data record using the Database Seeder file as follows :

    C:\programming\language\php\laravel\crud> php artisan db:seed --class=CategorySeeder  
    C:\programming\language\php\laravel\crud>   
    
  8. After executing the above command, just try to check the database again once more :

    mysql> select * from categories;
    +----+-------------+-----------+------------+------------+
    | id | name        | parent_id | created_at | updated_at |
    +----+-------------+-----------+------------+------------+
    |  1 | Application |         0 | NULL       | NULL       |
    |  2 | Database    |         0 | NULL       | NULL       |
    |  3 | Networking  |         0 | NULL       | NULL       |
    |  4 | Server      |         0 | NULL       | NULL       |
    +----+-------------+-----------+------------+------------+
    12 rows in set (0.71 sec)
    
    mysql>
    

Leave a Reply