Populate a Laravel database with seeders
Create Laravel seeders, compose model factories, and make database initialization repeatable while keeping destructive test resets isolated.
A Laravel seeder describes which data to insert and in what order. Use model factories to generate development or test records, then call the seeders from DatabaseSeeder. These examples follow Laravel 12.x.
Create and implement a seeder
php artisan make:seeder UserSeeder
Laravel creates database/seeders/UserSeeder.php. For disposable development data:
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
public function run(): void
{
if (! app()->environment(['local', 'testing'])) {
throw new \RuntimeException('Demo seeder requires local/testing');
}
User::factory(50)->create();
}
}
This requires a configured User model and factory. It creates fake users, not production administrator accounts. Running it again creates another 50 records.
Order dependent seeders
Inside DatabaseSeeder::run():
$this->call([
UserSeeder::class,
PostSeeder::class,
CommentSeeder::class,
]);
Define the referenced seeders first. Put prerequisite data before records that depend on it, and ensure the later seeders use valid related IDs.
php artisan db:seed
php artisan db:seed --class=UserSeeder
Check the active database connection before running either command. Environment guards help, but they do not replace database isolation.
Add a related-model factory
php artisan make:factory PostFactory --model=Post
In PostFactory, import App\Models\User and define fields matching the model's schema:
public function definition(): array
{
return [
'title' => fake()->sentence(),
'body' => fake()->paragraphs(3, true),
'user_id' => User::factory(),
];
}
Creating a post this way can create a related user. If you intend to reuse seeded users, supply that relationship explicitly instead of accidentally generating a new user for every post.
Reset only a disposable database
# Deletes every table on the target connection before reseeding.
php artisan migrate:fresh --seed
This is destructive and can remove tables beyond those created by this project. Use it only after confirming that the target is a dedicated, disposable development or test database.
For large datasets, create bounded batches. Bulk inserts can reduce overhead, but do not behave exactly like creating Eloquent models one by one: model events and other per-model behavior may be bypassed.
Common questions
Why is the seeder class missing? Check its filename, Database\Seeders namespace, and class name, then refresh Composer autoloading with composer dump-autoload if needed.
How do I avoid duplicate initialization rows? Define a real unique constraint and use firstOrCreate, updateOrCreate, or an appropriate upsert keyed by the business identifier. A count of all rows does not establish that the required reference data exists.
Can a seeder run in production? Yes. Laravel normally asks for confirmation there; --force bypasses that prompt. Review the selected seeder, destination, and rollback strategy. Do not use fake accounts or fixed example credentials as production initialization.
How do I target testing? php artisan db:seed --env=testing selects that environment, but verify the resolved connection and configuration cache. phpunit.xml is not automatically applied to ordinary Artisan commands. In database tests, configure the isolated database and use RefreshDatabase and $this->seed() as appropriate.
References: Laravel seeding and database testing.
Related reading
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan