Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
LaravelPHPJune 22, 20264 min read

Laravel Factories: A Beginner’s Guide to Dynamic Test Data

Laravel factories make php testing easier by generating dynamic data on the fly. Learn how to use model factories to speed up your local development workflow.

laravelphptestingeloquentdatabaseTutorial

Last month, I spent three hours debugging a failing test suite because my hard-coded database seeders couldn't handle a new edge case in our validation logic. I realized that manually managing test data was slowing my team down, so I finally leaned into laravel factories to automate the whole process.

If you’re still manually inserting records into your database for every test, you’re making your life harder than it needs to be. Factories allow you to define a blueprint for your models, giving you the power to generate hundreds of realistic records in a single line of code.

Why You Should Use Laravel Factories

When I first started with Laravel, I used to create generic test data in my setUp() method. It was messy, repetitive, and prone to "magic string" errors. By moving to model factories, I decoupled my test data from the specific test logic.

The biggest benefit? Speed. Instead of writing User::create(['name' => 'John', ...]) in every file, you define the structure once. If your database schema changes, you only update the factory, not fifty different test files.

Setting Up Your First Factory

Laravel uses the Faker PHP library under the hood to generate realistic data. To get started, make sure your model has the HasFactory trait:

PHP
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasFactory;
}

Once that's in place, you can generate a factory file using artisan: php artisan make:factory UserFactory. Inside the definition() method, you map your database columns to Faker methods:

PHP
public function definition(): array
{
    return [
        'name' => $this->faker->name(),
        'email' => $this->faker->unique()->safeEmail(),
        'password' => bcrypt('password'), #6A9955">// Or Hash::make('password')
        'created_at' => now(),
    ];
}

Using Laravel Database Testing Effectively

Now that your factory is defined, your laravel database testing becomes much cleaner. You can create a single user or a whole collection of them without any manual overhead.

Here is how I usually call these in my feature tests:

PHP
#6A9955">// Create a single user
$user = User::factory()->create();

#6A9955">// Create 10 users at once
$users = User::factory()->count(10)->create();

If you need to override a specific attribute—like testing a user who hasn't verified their email yet—you just pass an array:

PHP
$user = User::factory()->unverified()->create([
    'name' => 'John Doe',
]);

Advanced Techniques and Trade-offs

I’ve found that the real power comes from "State" methods. Instead of manually setting columns, define a state in your factory. For example, if you are working with Mastering Laravel Enums: A Guide to Type-Safe Data Modeling, you can create states for different user roles:

PHP
public function admin(): static
{
    return $this->state(fn(array $attributes) => [
        'role' => UserRole::Admin,
    ]);
}

However, don't over-engineer this. I once spent about two days building a complex tree of nested factories for a project with deep Laravel Eloquent Relationships: A Guide to Linking Data Models. It worked, but it made the tests brittle. If the parent model changed, the entire factory chain broke. Now, I try to keep factories focused on the model itself and use them alongside Mastering Laravel Database Seeders: Create Realistic Testing Data only when I need a base set of data for local development.

Frequently Asked Questions

How do I refresh the database between tests? Use the Illuminate\Foundation\Testing\RefreshDatabase trait in your test class. It rolls back the database after every test, ensuring a clean slate.

Can I use factories for non-model data? Technically, yes, but they are built for Eloquent. If you need to generate complex arrays for DTOs, consider using Mastering Laravel DTOs: Type-Safe Data Handling for Clean Code to keep your structure consistent.

Is Faker PHP still the standard? Yes, Laravel 11 continues to bundle Faker as the default, and it remains the best tool for generating everything from fake addresses to random timestamps.

Final Thoughts

Moving to factories was a turning point for my testing workflow. It shifted my focus from "how do I create this record" to "what is the specific condition I'm testing right now."

Next time, I might look into custom factory providers for more specialized data types, but for now, the standard faker methods cover about 95% of my needs. Start small, get your core models covered, and you'll see your test suite run faster and break less often.

Back to Blog

Similar Posts

LaravelPHPJune 23, 20264 min read

Mastering Laravel Local Scopes for Cleaner Database Filtering

Mastering Laravel local scopes helps you write cleaner, more readable database queries. Learn how to encapsulate filtering logic directly within your models.

Read more
Minimalist photo of a green notepad and pencil on a white background with a drawn question mark.
Laravel
PHP
June 21, 2026
4 min read

Mastering Laravel Eloquent Scopes: Writing Reusable Query Constraints

Mastering Laravel Eloquent scopes allows you to write cleaner database queries by encapsulating complex logic into reusable, readable model methods.

Read more
Two street cleaners working in Brasília, Brazil, wearing bright orange uniforms.
LaravelPHPJune 21, 20264 min read

Mastering Laravel Traits for Cleaner Eloquent Models

Master Laravel traits to simplify your Eloquent models. Learn how to implement effective PHP code reuse and follow DRY programming best practices today.

Read more