Laravel 8 Database Seeder Example

Laravel 8 Database Seeder Example

In this example we will learn Laravel 8 database seeder example. Laravel framework provide many functionalities to user to reduce developer’s time for developing website, here we will show How to create database seeder in Laravel 8.

Here, we will show Laravel 8 database seeder tutorial example, how to create seeder in laravel 8, how to use seeder in laravel 8, laravel 8 database seed tutorial, command to create seeder in laravel 8.

So, run bellow command in your terminal to create seeder in laravel 8 application.

php artisan make:seeder UserSeeder

Now will created one file UserSeeder.php on seeds folder. All seed classes are stored in the database/seeds directory.

Read Also :  Laravel 8 One to Many Eloquent Relationship Tutorial

Add your data as per your requirements such as bellow in this path database/seeds/UserSeeder.php

<?php
  
use Illuminate\Database\Seeder;
use App\Models\User;
   
class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::create([
            'name' => 'test',
            'email' => 'admin@gmail.com',
            'password' => bcrypt('123demo'),
        ]);
    }
}

Now run following command in your terminal

php artisan db:seed --class=UserSeeder

Or you can declare your seeder in DatabaseSeeder class file. then you have to run single command to run all listed seeder class.

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

and run following command in  your terminal.

php artisan db:seed

I hope it can help you…

Read Also :  Laravel 8 Automatic Database Encryption Decryption