Learn how to use database seeding and factories in Laravel to populate your application with realistic dummy data for testing and development.
Previously in this course, we covered working with Eloquent models to interact with your database. Now that you can save and retrieve records, you need a way to fill your application with data without typing it in manually every time you reset your database.
That is where seeding comes in. Seeding allows you to define a set of data that should exist in your database, making your development environment feel real and your testing process consistent.
When you are building your Task Manager, you need tasks to test your Blade loops and UI components. If you only have one task, you can’t verify if your pagination or filtering logic works.
Manual entry is error-prone and tedious. By using seeders, you create a "source of truth" for your development data. You can wipe your database and rebuild your entire state with a single command.
Laravel provides a dedicated directory for these files: database/seeders. To create a new seeder, use the Artisan command line:
Bashphp artisan make:seeder TaskSeeder
This creates a class in database/seeders/TaskSeeder.php. Inside, you’ll find a run() method. This is where you put your logic for inserting data.
PHPnamespace DatabaseSeeders; use Illuminate\Database\Seeder; use App\Models\Task; class TaskSeeder extends Seeder { public function run(): void { Task::create([ 'title' => 'Finish the Laravel course', 'description' => 'Complete the seeding lesson.', 'is_completed' => false, ]); } }
While you can hardcode data in a seeder, it's rarely enough. You often need 50 or 100 tasks to properly test your layouts. This is where Laravel factories become essential.
Factories use the Faker library to generate random, realistic data like names, sentences, or paragraphs. If you have already generated a model, you likely have a factory class in database/factories.
Open database/factories/TaskFactory.php and define the structure:
PHPpublic function definition(): array { return [ 'title' => fake()->sentence(), 'description' => fake()->paragraph(), 'is_completed' => fake()->boolean(), ]; }
Now, update your TaskSeeder to use this factory to generate multiple records:
PHPpublic function run(): void { #6A9955">// Generate 20 tasks \App\Models\Task::factory()->count(20)->create(); }
To execute your seeder, you run the following command:
Bashphp artisan db:seed --class=TaskSeeder
If you want to refresh your database entirely—wiping tables and re-running all migrations before seeding—you can combine the commands:
Bashphp artisan migrate:fresh --seed
This is a developer's best friend. Whenever you make a significant change to your database schema, you can reset everything to a known "good" state instantly.
TaskSeeder if you haven't already.TaskFactory and ensure it has definitions for title, description, and is_completed.TaskSeeder to create 50 tasks.php artisan migrate:fresh --seed and verify that your tasks table in your database browser (or via Tinker) is populated with 50 rows.DatabaseSeeder.php, you must call it explicitly in the DatabaseSeeder::run() method using $this->call(TaskSeeder::class); if you want it to run when you call php artisan db:seed without flags.Task::create() call fails, ensure your model has the $fillable property set correctly. We will cover this in detail in preventing mass assignment, but for now, remember that Eloquent requires you to "whitelist" fields you want to mass-assign.Seeding is about creating repeatable environments. By combining Seeders to organize your data logic and Factories to generate large volumes of realistic content, you ensure your application is always ready for testing. This workflow is the backbone of efficient, stress-free development.
Up next: Task Manager: Displaying Real Database Records
Laravel testing with Event::fake() helps you isolate logic by preventing side effects. Learn how to assert that events fired without triggering external code.
Seeding the Database
Protecting Routes with Middleware
Understanding CSRF Protection
Preventing Mass Assignment
Task Manager: Securing the Application
Introduction to Route Model Binding
Updating Existing Records
Deleting Records
Using Named Routes
Task Manager: Completing CRUD Functionality
Introduction to Database Relationships
Querying Related Data
Handling File Uploads
Using Flash Messages for User Feedback
Task Manager: Adding Status and Priorities
Introduction to Artisan Commands
Debugging with Laravel Tinker
Understanding Service Providers
Using View Composers
Task Manager: Refactoring for Clean Code
Introduction to Testing
Testing Forms and Validation
Using Database Transactions
Handling Global Exceptions
Preparing for Production
Environment Security Best Practices
Managing Assets in Production
Task Manager: Deployment Preparation