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 9 Inner Join Query Example
Laravel 9 Inner Join Query Exa...

In this article, we will see laravel 9 inner join query example. Also, see how to join two tables in laravel 9. In larav...

Read More

Mar-30-2022

How to Add Watermark on Image in Laravel 11
How to Add Watermark on Image...

Hello, laravel web developers! In this article, we'll see how to add a watermark on the image in laravel 11. He...

Read More

Aug-21-2024

How To Convert PHP Array To JSON Object
How To Convert PHP Array To JS...

In this article, we will explore the process of converting a PHP array into a JSON object. We'll achieve this transf...

Read More

Jul-08-2020

11+ Laravel Tips: Optimize Database Queries (2024)
11+ Laravel Tips: Optimize Dat...

Hey developers! If you're like me, constantly striving to make your Laravel applications faster and more effici...

Read More

Jan-05-2024