Would you ever like to fill your website with fake data quickly — like user accounts, dummy blog posts, or product listings?
Tada! Laravel has a cool feature called seeding. It helps you to fill your database with fake data in seconds.
Imagine it like planting seeds that grow into test data, so you don’t have to type it all manually!
In this Laravel 12.x tutorial, we’ll define Laravel database seeding in the easiest way possible — step-by-step, with instances.
What is Laravel Database Seeding?
Suppose you’re making a game and you want to test it with some sample players. You don’t need to add each player manually which consumes time!
While seeding, Laravel can automatically do that for you!
- It adds fake data to your database
- It’s super fast and saves you time
- Great for testing and development
Step 1: Create a Laravel Project (If You Don’t Have One)
Let’s begin afresh. Open your terminal and run:
bash composer create-project laravel/laravel laravel-seeding-demo cd laravel-seeding-demo php artisan serve
Laravel 12 is now up and running!
Step 2: Create a Model and Migration
Try to make a simple Post model for blog posts.
bash php artisan make:model Post -m
This creates:
- A Post model
- A migration file to create the database table. You can find more about Larvel Migration
Open the migration file in database/migrations/ + your newly created post migration file and add:
php Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); }); Then run:
bash php artisan migrate Your posts table is now in the database.
Step 3: Create a Factory (To Make Fake Data)
Laravel uses factories to create fake content as names, emails, and titles.
Create a factory for the Post model:
bash php artisan make:factory PostFactory --model=Post
Open the factory file: database/factories/PostFactory.php
Update it like this:
php public function definition(): array { return [ 'title' => fake()->sentence(), 'body' => fake()->paragraph(), ]; }
The fake() function will create random sentences and paragraphs every time — as a magic!
Step 4: Create a Seeder
Use seeder to build a plant the data in our database.
Run this command:
bash php artisan make:seeder PostSeeder
Open database/seeders/PostSeeder.php and add:
php use App\Models\Post; public function run(): void { Post::factory()->count(10)->create(); }
This line tells Laravel: “Hey Laravel, please make 10 fake posts using the PostFactory!”
Step 5: Run the Seeder
Before running the seeder, open DatabaseSeeder.php and tell it to call PostSeeder:
php public function run(): void { $this->call(PostSeeder::class); }
Now run the seeder:
bash php artisan db:seed
You just created 10 blog posts in your database with one line of code, Congrats! Magic, No typing, no copy-paste, just Laravel!
Step 6: Refresh and Seed Together (Optional)
If you want to clear your database and start fresh with seeding:
bash php artisan migrate:refresh –see
This requires 3 things:
- Clears the tables
- Re-runs all migrations
- Re-runs your seeder to fill the database again
Bonus: Seeder for Users or Products
Want to create fake users? Do the same thing with the built-in User model:
bash php artisan tinker
Open \App\Models\User::factory()->count(5)->create();
Want to seed products?
- Make a Product model
- Create a ProductFactory
- Write a ProductSeeder
- Call it from php
- Seed it with php artisan db:seed
Flexible steps!
Read More Blog Posts
How to use Laravel Queues Beginner’s Guide
Tips and Tricks for Running Commands with Laravel Process
Laravel Notifications Made Easy: A Step-by-Step Guide
Recap—What Did You Learn?
What Laravel database seeding is
How to create fake data with factories
How to use seeders to fill the database
How to use Laravel 12 seeding commands
How to reset and seed again for testing
Common Laravel Seeding Commands
Command |
What It Does |
php artisan db:seed | Run all seeders in DatabaseSeeder.php |
php artisan make:seeder Name | Create a new seeder class |
php artisan make:factory | Create a new factory |
php artisan migrate:refresh –seed | Reset database and re-run seeder |
php artisan tinker | Test factories and models manually |
Final Words
You’ve just included one of the most powerful tools to the Laravel toolbox.
Next steps you can try:
- Seed multiple models (like users, posts, products)
- Use relationships in factories (e.g., posts that belong to users)
- Write custom seeders with loops or conditions
Awesome experience!