What is seeding?
Seeding is the process of populating your database with sample or initial data.
Development environments and automated tests often require data to already exist. Entering data manually every time is tedious, so seeders let you define that data once and run it repeatedly.
Seeder classes are stored in the database/seeders directory. Laravel provides a DatabaseSeeder class by default.
Creating a seeder
Use the make:seeder Artisan command to generate a new seeder class:
php artisan make:seeder UserSeeder
The generated file is placed at database/seeders/UserSeeder.php:
<? php
namespace Database\Seeders ;
use Illuminate\Database\ Seeder ;
class UserSeeder extends Seeder
{
/**
* Run the database seeders.
*/
public function run () : void
{
// Add your data insertion logic here
}
}
Implementing a seeder
Write your data insertion logic inside the run() method. You can use the DB facade or Eloquent models.
Using the DB facade
<? php
namespace Database\Seeders ;
use Illuminate\Database\ Seeder ;
use Illuminate\Support\Facades\ DB ;
use Illuminate\Support\Facades\ Hash ;
class UserSeeder extends Seeder
{
public function run () : void
{
DB :: table ( 'users' ) -> insert ([
[
'name' => 'Alice Smith' ,
'email' => '[email protected] ' ,
'password' => Hash :: make ( 'password' ),
'created_at' => now (),
'updated_at' => now (),
],
[
'name' => 'Bob Jones' ,
'email' => '[email protected] ' ,
'password' => Hash :: make ( 'password' ),
'created_at' => now (),
'updated_at' => now (),
],
]);
}
}
Using Eloquent models
use App\Models\ User ;
use Illuminate\Support\Facades\ Hash ;
public function run () : void
{
User :: create ([
'name' => 'Admin' ,
'email' => '[email protected] ' ,
'password' => Hash :: make ( 'password' ),
]);
}
Mass assignment protection is automatically disabled during database seeding, so you don’t need to worry about $fillable or $guarded settings.
Using model factories
When you need large amounts of test data, combine seeders with model factories . Factories generate realistic fake data in bulk.
use App\Models\ User ;
use App\Models\ Post ;
public function run () : void
{
// Create 10 users
User :: factory ( 10 ) -> create ();
// Create 5 users, each with 3 related posts
User :: factory ( 5 )
-> hasPosts ( 3 )
-> create ();
}
See the Eloquent factory documentation for details on defining and customizing factories.
Using DatabaseSeeder
DatabaseSeeder is the entry point that coordinates multiple seeders. Use the call() method to specify which seeders to run:
<? php
namespace Database\Seeders ;
use Illuminate\Database\ Seeder ;
class DatabaseSeeder extends Seeder
{
public function run () : void
{
$this -> call ([
UserSeeder :: class ,
PostSeeder :: class ,
CommentSeeder :: class ,
]);
}
}
Seeders run in the order you list them. When foreign key constraints exist, seed the referenced table first (for example, users before posts).
Running seeders
Run all seeders
This calls DatabaseSeeder, which in turn runs all seeders registered with call().
Run a specific seeder
Use the --class option to run a single seeder class:
php artisan db:seed --class=UserSeeder
Running with migrations
Combine the migrate:fresh command with the --seed option to drop all tables, re-run every migration, and then seed in one step:
php artisan migrate:fresh --seed
To run a specific seeder instead of DatabaseSeeder:
php artisan migrate:fresh --seed --seeder=UserSeeder
migrate:fresh drops and recreates all tables, destroying any existing data. Never run this in production.
Running seeders in production
Running seeders in the production environment prompts you to confirm. To skip the prompt, use the --force flag:
php artisan db:seed --force
Seeding in production can overwrite or destroy data. Always take a backup before running seeders against a production database.
Muting model events
To prevent model events (creating, created, etc.) from firing during seeding, apply the WithoutModelEvents trait:
<? php
namespace Database\Seeders ;
use Illuminate\Database\ Seeder ;
use Illuminate\Database\Console\Seeds\ WithoutModelEvents ;
class DatabaseSeeder extends Seeder
{
use WithoutModelEvents ;
public function run () : void
{
$this -> call ([
UserSeeder :: class ,
]);
}
}
This also applies to any child seeders called via call().
Practical example: blog application
Here is a complete seeding setup for an application with users and posts:
// database/seeders/UserSeeder.php
class UserSeeder extends Seeder
{
public function run () : void
{
User :: factory ( 10 ) -> create ();
}
}
// database/seeders/PostSeeder.php
class PostSeeder extends Seeder
{
public function run () : void
{
// Create 2–5 posts for each user
User :: all () -> each ( function ( $user ) {
Post :: factory ( rand ( 2 , 5 )) -> create ([
'user_id' => $user -> id ,
]);
});
}
}
// database/seeders/DatabaseSeeder.php
class DatabaseSeeder extends Seeder
{
public function run () : void
{
$this -> call ([
UserSeeder :: class ,
PostSeeder :: class , // Run after UserSeeder
]);
}
}
Then reset and seed everything at once:
php artisan migrate:fresh --seed
Next steps
Eloquent ORM Learn how to query and work with the data you seeded using Eloquent.