Laravel 8 Seeder Example Tutorial


Share:
Published By: Admin, Published On: May 07, 2021, Category: Laravel 8 Laravel

Hello Friends,

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

Here i will give you few step instruction to create seeder in laravel 8.

Create Seeder Command:

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 way to run seeder in laravel 8.

Way 1: Run Single Seeder

You need to run following command to run single seeder:

php artisan db:seed --class=ClientUserSeeder
Way 2: 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....

Share: