Laravel 8 Database Seeder Example

WebSolutionStuff | Oct-23-2020 | Categories : Laravel MySQL

Today we will see Laravel 8 Database Seeder Example, as we all know laravel framework provide many functionalities to user to reduce developer's time for developing website, here we will see How to create database seeder in Laravel 8.

Many times you have requirment to add some default records or entry to login or add form details etc. there are no problem to add manual data in database one or two times but it is very challenging task to add data manually each and every time. So, if you want to avoid this boring task you need to create database seeders in laravel, laravel provides inbuilt command to create database seeders in laravel 8.

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

php artisan make:seeder UserSeeder

 

 

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

Add your data as per your requirments like below 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' => '[email protected]',
            'password' => bcrypt('123demo'),
        ]);
    }
}

Now run below 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 below command in  your terminal. 

php artisan db:seed

 

Featured Post




Follow us
facebooklogo github instagram twitter