Laravel 12 Generate Fake Data using Factory Tinker

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

Laravel 12 Generate Fake Data

 

Step 1: Create a Factory

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.

 

Step 2: Define Fake Data in Factory

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),
    ];
}

 

Step 3: Open Laravel Tinker

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.

 

Step 4: Create Fake Data for Other Models

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).

 

Optional: Use Seeder Instead of Tinker

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:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS