🌱 Laravel 12 Database Seeder Tutorial

Database seeding in Laravel is a convenient feature that allows you to populate your database with default or sample data. Whether you're testing, developing, or setting up an application, seeders can save you time and effort.

In this tutorial, we will walk through how to use seeders in Laravel 12 with real-world examples.


🔍 What is a Database Seeder?

A seeder is a class in Laravel that helps you insert sample or default records into your database tables automatically. It’s especially useful for:

  • Populating test data for development
  • Inserting default users (like an admin)
  • Seeding data for dropdowns, roles, permissions, etc.


✅ Prerequisites

Before we begin, make sure:

  • Laravel 12 is installed
  • Your database is configured in the .env file
  • You have run the migrations:

php artisan migrate

🧰 Step 1: Create a Seeder

Use Artisan to create a seeder class:

php artisan make:seeder UserSeeder

This will generate a file in:

database/seeders/UserSeeder.php

📝 Step 2: Write Seeder Logic

Open UserSeeder.php and insert the data logic inside the run() method:

<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use App\Models\User; use Illuminate\Support\Facades\Hash; class UserSeeder extends Seeder { public function run(): void {
        $userArray = array( 'name' => 'Admin User', 'email' => 'admin@example.com', 'password' => Hash::make('password'), );
        if(!User::where('email',$userArray['email'])->exists()){     User::create($userArray);
        }
// Optional: Generate multiple users using factory //User::factory()->count(10)->create(); } }

📂 Step 3: Register Seeder in DatabaseSeeder

Open the main DatabaseSeeder.php file located in database/seeders/ and register your new seeder:

<?php namespace Database\Seeders; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run(): void { $this->call([ UserSeeder::class, ]); } }

🚀 Step 4: Run the Seeder

To seed your database, run:

php artisan db:seed

To run a specific seeder:

php artisan db:seed --class=UserSeeder

🧪 (Optional) Create and Use Factory

If you want to use Laravel's Factory to generate fake data:

php artisan make:factory UserFactory --model=User

Now, edit database/factories/UserFactory.php:

<?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; class UserFactory extends Factory { public function definition(): array { return [ 'name' => $this->faker->name(), 'email' => $this->faker->unique()->safeEmail(), 'password' => bcrypt('password'), 'remember_token' => Str::random(10), ]; } }

Now you can generate 10 users easily:

User::factory()->count(10)->create();

🔄 Refresh & Seed the Database

You can reset your database and run seeders in one go using:

php artisan migrate:fresh --seed

Or if you want to keep data and rerun seeders only:

php artisan db:seed

🏗️ Seeding Multiple Tables

You can create multiple seeders like PostSeeder, ProductSeeder, etc., and register all in DatabaseSeeder.php:

$this->call([ UserSeeder::class, ProductSeeder::class, CategorySeeder::class, ]);

📌 Use Cases of Seeders

  • Testing user login with predefined credentials
  • Seeding roles and permissions
  • Adding sample blog posts, products, or categories
  • Initial setup for admin dashboards


✅ Final Tips

  • Use php artisan migrate:fresh --seed often during development.
  • Keep your seeder data realistic using Laravel’s Faker.
  • Do not run seeders in production unless absolutely required.


📚 Conclusion

Laravel 12 makes database seeding simple and efficient. Whether you’re building a new app or working in a team, seeders help maintain consistent data across environments and streamline the development process.

With just a few commands, you can have a fully populated database ready for testing.


🧩 Example File Structure

project/ ├── database/ │ ├── factories/ │ │ └── UserFactory.php │ ├── seeders/ │ │ ├── UserSeeder.php │ │ └── DatabaseSeeder.php

Post a Comment

Previous Post Next Post

Contact Form