DEV Community

loading...

Laravel 8 Database Seeder Example

Websolutionstuff
I am Laravel Developer. I have good knowledge of JavaScript, JQuery, Bootstrap and REST API.
Originally published at websolutionstuff.com ・2 min read

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 requirement 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
Enter fullscreen mode Exit fullscreen mode

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 requirements 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' => 'admin@gmail.com',
            'password' => bcrypt('123demo'),
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now run below command in your terminal

php artisan db:seed --class=UserSeeder
Enter fullscreen mode Exit fullscreen mode

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

and run below command in your terminal.

php artisan db:seed
Enter fullscreen mode Exit fullscreen mode

Read Also : Cron Job Scheduling In Laravel


Read Also : Laravel 8 CRUD Operation Example


Read Also : Create Dummy Data Using Laravel Tinker

Thanks for reading...!!
If you like this article then don't forget to like, share and comment.

Discussion (0)