Laravel 8.x Tips to Create Database Seeder

In this quick example i will discuss about laravel seeder example. So in this laravel seeder example you will learn how to insert dummy data into our database quickly in laravel 8. I will explain step by step tutorial laravel 8 seeder example so i will help you laravel 8 database seeder example.

Sometimes we need to check our project with huge data or you need some dummy data to test your project. Then this situation laravel database seeder is the way that you can do. Laravel introduce seeder for creating testing data for our project.

Let's see how we can create laravel create seeder from database.

Step 1 : Create Seeder Class

Laravel gives us nice command to create seeder in laravel. so you can run following command to make seeder in laravel application.

php artisan make:seeder AdminUserSeeder

 

After doing it, it will create one file AdminUserSeeder.php on seeds folder. Open this file and update it like below

database/seeds/AdminUserSeeder.php

use Illuminate\Database\Seeder;
use App\Models\User;
   
class AdminUserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::create([
            'name' => 'CodeChief',
            'email' => '[email protected]',
            'password' => bcrypt('password'),
        ]);
    }
}

 

Step 2 : Run Seeder

There is a two way to run this seeder in Laravel 8. In this laravel seeder example i will give you both way to run seeder in laravel 8.

You have to run following command to run single seeder:

php artisan db:seed --class=AdminUserSeeder

 

In the second step, you have to declare your seeder in DatabaseSeeder class file. After that you have to run single command to run all listed seeder class.

database/seeds/DatabaseSeeder.php

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

 

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

php artisan db:seed

 

Recommended : Laravel Tips to Set Foreign Key in Laravel Migration

 

Hope this Laravel 8 seeder example tutorial will help you.