Laravel Database Seeder Example Tutorial

Using this Laravel Seeder Example you will learn how to create and use database seeder in the Laravel application. Laravel provides us with a simple method make:seed to insert multiple testing data in database table. We can make a Laravel database seeder after running the migration of the table. All seed classes are stored in the database/seeds directory.

How to Create Database Seeder in Laravel Example

About Laravel Database Seeder

Laravel Seeder is a powerful feature that allows developers to populate their application’s database with dummy data. This feature is particularly useful for testing and development purposes.

Seed classes may have any name you wish, but probably should follow some sensible convention, such as UserSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

Let’s see the step-by-step example on how to insert data using seeder in Laravel.

1. Download Laravel App

First, Download a fresh Laravel application using the followinf command.

composer create-project laravel/laravel laravel-database-seeder --prefer-dist

Next, go inside the app

cd laravel-database-seeder

2. Setup Database Credentials

Now, open the .env file and place the following code, it makes the frequent connection between laravel and the database to manage the data.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Now run the following command in the terminal, its generate the prebuild “users” table migration to add tables in the database.

php artisan migrate

3. Create Seeder in Laravel

Now, you need to create a database seeder file. To make a seeder class in laravel you need to use the following command.

php artisan make:seeder UserSeeder

Now navigate to the “database/seeds” directory you will find a new file generated there named “UserSeeder”. So open the “database/seeds/UserSeeder.php” and update the following code on it.

<?php 
use Illuminate\Database\Seeder; 
use App\Models\User; 
class UserSeeder extends Seeder { 
    /** 
    * Run the database seeds. 
    * 
    * @return void 
    */
   public function run() { 
           User::truncate(); 
           $users = [ 
            [ 
              'name' => 'Super Admin',
              'email' => 'superadmin@gmail.com',
              'password' => '123456',
            ],
            [
              'name' => 'User',
              'email' => 'user@gmail.com',
              'password' => '13456',
            ],
             [
              'name' => 'Client',
              'email' => 'client@gmail.com',
              'password' => '13456',
            ] 
          ];

          foreach($users as $user)
          {
              User::create([
               'name' => $user['name'],
               'email' => $user['email'],
               'password' => Hash::make($user['password'])
             ]);
           }

    }
}

You may specify to run a specific seeder class to run individually using the –class option. So run the user seeder and update the test data to database.

php artisan db:seed --class=UserSeeder

You can seed multiple seeders just by running the below command but you need to initialize your seed in the database\seeds\DatabaseSeeder.php file. So, open the database\seeds\DatabaseSeeder.php file and put the below code on it;

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UserSeeder::class);
        // $this->call(PostSeeder::class);
    }
}

After initializing this you can run the below command to seed.

php artisan db:seed

Generate Dummy Data in Laravel Seeder Using Faker

we can generate dummy data using model Factories and faker to create fake data (with relations etc) for developing and testing your app. Here you need to use faker class for generation testing users.

<?php
  
use Illuminate\Database\Seeder;
use App\User;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        for ($i=0; $i < 10; $i++) { 
	    	User::create([
	            'name' => str_random(8),
	            'email' => str_random(12).'@mail.com',
	            'password' => Hash::make('12345678'),
	        ]);
    	}
    }
}

Run seeder

php artisan db:seed --class=UserSeeder

See Also: Generate Fake Data using Faker and Factory in Laravel

After running the php artisan db:seed –class=UserSeeder you can check the dummy records created in your users table.

Finally, the Laravel Database Seeder tutorial with examples has been completed. I hope you enjoy this tutorial.

2 thoughts on “Laravel Database Seeder Example Tutorial”

Leave a Comment