Laravel 8 Database Seeder Example

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

In this article, we will see the laravel 8 database seeder example. As we all know laravel framework provides many functionalities to the user to reduce the developer's time for developing a website. Here, we will see how to create database seeder in laravel 8.

Many times you have a requirement to add some default records or entries to log in or add form details etc. there is no problem adding manual data in the database one or two times but it is a 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 an inbuilt command to create database seeders in laravel 8.

So, let's see how to create seeder in laravel 8 and laravel 8 seeder multiple records.

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

php artisan make:seeder UserSeeder

 

 

Now, it will create one file UserSeeder.php in the 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' => '[email protected]',
            'password' => bcrypt('123demo'),
        ]);
    }
}

Now, run the below command in your terminal.

php artisan db:seed --class=UserSeeder

You can declare your seeder in the DatabaseSeeder class file. then you have to run a single command to run all listed seeder classes.

<?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 the below command in your terminal. 

php artisan db:seed

 


You might also like:

Recommended Post
Featured Post
Laravel 8 QR Code Generator Example
Laravel 8 QR Code Generator Ex...

In this tutorial, we will see the laravel 8 QR code generator example. we will generate QR code using simpleso...

Read More

Jan-24-2022

Laravel 8 Toastr Notifications Example
Laravel 8 Toastr Notifications...

Today, I will show you Laravel 8 Toastr Notifications Example. There are many types of notifications availa...

Read More

Oct-19-2020

Reseller Hosting Myths to Know and Unfollow
Reseller Hosting Myths to Know...

If you work in the web hosting sector, you're probably acquainted with the term reseller hosting. Resellers make up...

Read More

Apr-07-2022

How to Create Components in Angular 16
How to Create Components in An...

In this article how to create components in angular 16. Here, we will learn about how to use angular component...

Read More

Jun-02-2023