Custom Factory States in Laravel: How to Use unverified() and Create Your Own

If you’ve worked with Laravel, you know that factories are a powerful way to generate test data. But did you know you can define custom factory methods, like unverified()?

In this post, we’ll explore what factory states are, how Laravel’s default unverified() works, and how you can create your own.

What is a Factory State?

A factory state is a method in a Laravel factory that modifies the default attributes when creating a model. Think of it as a “preset” for specific scenarios.

For example, Laravel’s default UserFactory might create users like this:

User::factory()->create();

This will generate a user with email_verified_at set to now(), meaning the email is verified. But what if you need a user who hasn’t verified their email? That’s where unverified() comes in.


Understanding the unverified() Method

Here’s how unverified() is defined in the UserFactory:

public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}

🔹 What Happens Here:

  • state() modifies attributes of the model temporarily.
  • email_verified_at is set to null to simulate an unverified user.
  • You can now use it like this:
User::factory()->unverified()->create();

This creates a user who has not verified their email, perfect for testing verification flows or middleware like verified.


Creating Your Own Custom Factory States

The real power comes when you define your own states. For example, suppose you want a “premium” user:

public function premium(): static
{
return $this->state(fn (array $attributes) => [
'is_premium' => true,
'subscription_ends_at' => now()->addYear(),
]);
}

Then in your seeder:

User::factory()->premium()->count(5)->create();

This will create five premium users with the subscription ending one year from today.


🔹 Chaining States

You can even chain multiple custom states:

User::factory()->unverified()->premium()->create();

This creates a user who is both unverified and premium. Chaining is very flexible and lets you simulate almost any scenario.


Why Use Factory States?

  • Simplifies test setup.
  • Makes seeders more readable and maintainable.
  • Simulates real-world scenarios, like unverified users, admin users, or premium subscribers.
  • Keeps your factories DRY — no need to repeat attributes everywhere.

Quick Tips

  1. Factory states return $this->state(), so you can chain them.
  2. You can define as many states as you need.
  3. Use them in seeders and tests interchangeably.
  4. Always name states clearly (unverified, premium, admin) so your code is self-explanatory.

Conclusion

Laravel’s factories are not just for generating random data—they’re for modeling real scenarios. By leveraging factory states like unverified() or creating your own, you can make your tests and seeders more expressive, readable, and maintainable.

Next time you need a specific type of user or model, remember: just define a state and chain it!