what is seeder in laravel

working seeder with Database in Laravel

Seeders allow you to insert dummy or sample data into your database using PHP code, especially during development.

 It helps developers quickly fill the database with test data for development or testing purposes, without manually adding records.

  • Imagine you're opening a new restaurant and want to check how it looks before guests arrive.
  • So, you ask your chef to place sample food on each table — just for show and testing.
  • In Laravel, the seeder acts like that chef who pre-fills your database tables with sample or dummy data.
  • It helps developers test layouts, features, and queries without manually adding real data.
  • You can reset and refill data anytime during development using seeders.
  • Seeders are just PHP classes that run insert queries or call factories.
  • Used mainly in development, testing, or demo phases of a project.


how to create and run a Seeder in laravel

1. Create Database

Step: Create a database in MySQL (e.g., store).


2. Database Connection


  1. Open your Laravel project folder.
  2. Open the .env file.
  3. Set the database name:
    DB_DATABASE=store   # [e.g., your database name]
    

3. Create Migration


a. Open Terminal

Press Ctrl + Shift + ` (backtick) to open the terminal in VS Code.

b. Create Migration

Run this command to create a migration file:

php artisan make:migration create_students_table

c. Open Migration File

Go to:
📂 database/migrations/xxxx_xx_xx_xxxxxx_create_students_table.php

d. Write Code in up Function:

Open the file and edit it like this:

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

return new class extends Migration {
    public function up() {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('rollno');
            $table->timestamps();
        });
    }

    public function down() {
        Schema::dropIfExists('students');
    }
};

e. Run Migration

Execute this command to apply the migration:

php artisan migrate

4. Create Model


a. Open Terminal

Press Ctrl + Shift + ` (backtick) to open the terminal.

b. Create Model

Run this command:

php artisan make:model Student

5. Create Seeder


a. Open Terminal

Press Ctrl + Shift + ` (backtick) to open the terminal.

b. Create Seeder

Run this command:

php artisan make:seeder StudentSeeder

6. Write Entries in Seeder (3 Entries)


a. Open Seeder File

Go to: database/seeders/StudentSeeder.php

b. Write This Code in run Function:

<?php

namespace Database\Seeders;

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

class StudentSeeder extends Seeder
{
    public function run()
    {
        Student::insert([
            ["name" => "Sagar", "rollno" => 302],
            ["name" => "Rahul", "rollno" => 303],
            ["name" => "Amit", "rollno" => 304]
        ]);
    }
}

7. Register Seeder in DatabaseSeeder.php


a. Open DatabaseSeeder.php File

Go to:
📂 database/seeders/DatabaseSeeder.php

b. Add This Code in run Function:

<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([
            StudentSeeder::class,
        ]);
    }
}

8. Run Seeder


Run this command to insert the data:

php artisan db:seed


0 Comments