How to Create Database Seeder in Laravel 9 Application

Throughout this tutorial, you will learn about seeding your database with test data using Laravel built-in seed classes.

This quick laravel database seeder example invokes all the know-hows of including and inserting fake data records into the database in the laravel application.

Developing an application goes through many hits and trials, particularly when you need extensive records present in your database to check your functionalities’ outcome.

We want to make testing data exorbitantly facile by offering you how to solve adding fake data into laravel. Laravel is a robust framework which solves almost all your problem by its documentation paradigm.

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeders directory. 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.
– Laravel

So, you will learn everything about creating a laravel database seeder and adding fake records into your database table.

Create Laravel App

Begin the journey with creating a fresh laravel application with composer command:

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

Making Database Ready

Start your local development server, move to .env file at your project root. Define database name, username and password.

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

Create Model and Run Migration

You need to add a new Student table into your database; create a Student model.

php artisan make:model Student -m

Define table values in database/migrations/timestamp_students_table.php:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('phone');
            $table->string('email')->unique();                        
            $table->string('dob');
            $table->string('stream');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}

Similarly, define the Student table values in app/Models/Student.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    use HasFactory;
    public $fillable = [
        'name',
        'phone',
        'email',     
        'dob',   
        'stream',
    ];    
}

All set, go ahead and run migration:

php artisan migrate

Create Laravel Seeder Class

With just one power-packed command, you can make a seeder class in Laravel; it goes as below:

php artisan make:seeder StudentSeeder

Here is the seeder configuration file created in database/seeds/StudentSeeder.php.

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Student;


class StudentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Student::create([
            'name' => 'John Addison',
            'phone' => '88992299112',
            'email' => 'john@yahoo.com',
            'dob' => '2011-02-04',
            'stream' => 'Physics',
        ]);
    }
}

Run Seeders

In laravel, we have two methods through which you can run the seeder in the Laravel application. Let’s have a look at both the methods profoundly.

Given command lets you create single seeder:

php artisan db:seed --class=StudentSeeder

The second method is to go to database/seeds/DatabaseSeeder.php and define the Seeder class inside the run() function with the call method.

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(StudentSeeder::class);
    }
}

Eventually, you need to execute the below command to create all the listed seeders.

php artisan db:seed

Subsequently, we have completed the Laravel seeder tutorial, and i hope you would have understood the entire concept.