Laravel 9 Seeder Example Tutorial

May 14, 2022 . Admin

Hello Friends,

Now let's see an example of how to create seeder in laravel 9 example. This is a short guide on laravel 9 database seeder. Here you will learn how to create a seeder. We will use how to create seeder in laravel 9. Let's get started with how to create seeder in laravel 9.

Here I will give you a few step instruction to create seeder in laravel 9.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Seeder
php artisan make:seeder ClientUserSeeder

after run above command, it will create one file ClientUserSeeder.php on seeds folder. All seed classes are stored in the database/seeds directory.

database/seeds/ClientUserSeeder.php
<?php

use Illuminate\Database\Seeder;
use App\Models\Employee;
   
class ClientUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Employee::create([
            'name' => 'abc',
            'email' => 'client@gmail.com',
            'password' => bcrypt('123456'),
        ]);
    }
}

There is a two-way to run this seeder. I will give you both ways to run seeder in laravel 9.

Step 3: Run Single Seeder

You need to run following command to run single seeder:

php artisan db:seed --class=ClientUserSeeder
Step 4: Run All Seeders

In this way, you have to declare your seeder in DatabaseSeeder class file. then you have to run single command to run all listed seeder class.

database/seeds/DatabaseSeeder.php
<?php
  
use Illuminate\Database\Seeder;
   
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(ClientUserSeeder::class);
    }
}

Now you need to run following command for run all listed seeder:

php artisan db:seed

It will help you....

#Laravel 9