Hey! In this article, I’ll show you how I use Laravel 12 factories and Tinker to generate fake data — perfect for testing layouts, tables, and APIs without manually adding records.
With a few commands, you can create hundreds of dummy users, posts, or products in seconds. Laravel uses Faker under the hood, so the data looks real enough for testing.
Laravel 12: Generate Fake Data
Let’s say we want to create fake users. First, run:
php artisan make:factory UserFactory --model=User
This creates a new file at database/factories/UserFactory.php
.
Open UserFactory.php
and update the definition()
method like this:
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => bcrypt('password'),
'remember_token' => Str::random(10),
];
}
Now use Laravel Tinker to generate fake users:
php artisan tinker
Once inside Tinker, run this to create 10 users:
User::factory()->count(10)->create();
You’ll instantly see 10 rows added to your users
table.
You can repeat this process for any model — just make a factory like this:
php artisan make:factory PostFactory --model=Post
Then define the fields and use Tinker to generate:
Post::factory()->count(5)->create();
Make sure the table has matching fields and that your model allows mass assignment (fillable
or guarded
is set properly).
If you want to automate data generation, create a seeder:
php artisan make:seeder UserSeeder
Then in run()
method:
public function run()
{
\App\Models\User::factory()->count(50)->create();
}
Run it with:
php artisan db:seed --class=UserSeeder
Or include it in DatabaseSeeder.php
to run all together.
That’s it! Now you know how to generate fake data using Laravel 12 factories and Tinker. It’s one of the best ways to test UI, APIs, and relationships during development.
I use this setup every time I start a new project — saves time and keeps your dev DB filled with realistic test data.
You might also like: