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
Send Email In Laravel
Send Email In Laravel

In this article, we will explore the process of sending emails in Laravel, covering versions 6, 7, 8, 9, and 10. Email f...

Read More

Sep-02-2020

Disable Sorting On Specific Columns In Datatable
Disable Sorting On Specific Co...

In this article, we will delve into the process of disabling sorting for specific columns in Datatables. If you find the...

Read More

Aug-24-2020

Laravel 8 Ajax CRUD With Yajra Datatable
Laravel 8 Ajax CRUD With Yajra...

In this tutorial, we will see laravel 8 ajax crud with yajra datatable. I will show you how to create ajax crud ope...

Read More

Jan-05-2022

How To Create Candlestick Chart In Laravel 9 Using Highcharts
How To Create Candlestick Char...

In this article, we will see how to create a candlestick chart in laravel 9 using highcharts. A candlestick is a ty...

Read More

Oct-06-2022