Seeding With Factories in Laravel

Yesterday, we talked about how Factories can be used to generate test objects. Going a step further, any time you need to populate your database with test records, Seeders use factories to produces realistic values that match your expected schema.

You wire your seeders into database/seeders/DatabaseSeeder.php, and from there a single php artisan db:seed gives you a fully populated database — perfect for onboarding new developers or running your test suite against data that simulates real usage.

Here’s what a simple User seeder might look like:

namespace Database\Seeders;

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

class UserSeeder extends Seeder
{
    public function run(): void
    {
        // Create a known user for local dev/testing
        User::factory()->create([
            'name' => 'Test User',
            'email' => '[email protected]',
        ]);

        // Generate 50 random users
        User::factory()->count(50)->create();
    }
}

This gives you one predictable user you can always log in with during development, plus 50 randomized users to fill out the app. The factory handles generating realistic names, emails, and hashed passwords behind the scenes — you just tell the seeder how many and what overrides you need.