Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kiwi0134's avatar

Valid use cases for model factories?

Hello everyone,

I got the following thoughts: What are valid use-cases for model factories? The factory pattern in itself is not exclusive to tests and Laravel does not put those factories into the Tests directory. This leads me to suspicion that factories are also not exclusive to tests in Laravel.

Would you consider a valid use case for factories to also create probably complex models inside your applications? Especially with custom factory methods, I think that this might be a huge benefit. I haven't used factories outside of tests, yet.

Do you use them throughout your application, outside of tests? If so, how do you use them and if not, why?

I'd love to hear your thoughts about this.

1 like
8 replies
LaryAI's avatar
Level 58

Model factories in Laravel are primarily designed for testing purposes, allowing developers to quickly create objects with dummy data that adhere to the model's specifications. However, they can also be used outside of testing in certain scenarios. Here are some valid use cases for model factories outside of tests:

  1. Seeding the Database: When you need to seed your database with sample data, model factories can be used to generate this data. This is particularly useful during development or when setting up a demo version of your application.
use App\Models\User;

User::factory()->count(50)->create();
  1. Prototyping: During the early stages of development, you might want to quickly prototype features without setting up complex data creation workflows. Factories can help you create the necessary objects on the fly.

  2. Development Workflows: Sometimes, you might need to create objects with a complex set of states for development purposes. Custom factory states can simplify this process.

// Custom factory state for a user with a premium account
User::factory()->premium()->create();
  1. Local Testing and Debugging: When debugging an issue locally, you might want to generate a specific scenario quickly. Using factories can help you recreate the scenario without manually inserting data into the database.

  2. Dynamic Mocking: If your application has a feature that requires dynamic content generation (e.g., a CMS or a blog), you could use factories to create this content on the fly in a non-production environment.

It's important to note that while factories can be used outside of testing, they should be used with caution. Here are some considerations:

  • Performance: Creating models through factories can be slower than using raw SQL, especially when dealing with large volumes of data.
  • Business Logic: Factories should not replace proper business logic for creating models. In production code, you should use service classes, repositories, or similar patterns to encapsulate the logic for creating complex models.
  • Clarity and Maintenance: Using factories outside of their intended context (testing and seeding) can lead to confusion among team members and can make the codebase harder to maintain.

In conclusion, while it's technically possible to use model factories throughout your application, it's generally best to restrict their use to testing and database seeding. If you find yourself needing to use factories in production code, it might be a sign that you should refactor your code to better handle model creation through dedicated classes or methods that encapsulate the necessary business logic.

gych's avatar

Its not in the tests folder because a factory is not an actual test.

The factories are used to seed the database with fake data so you can see your application as how it would be when its actually in use and filled with a lot of data. Creating all this data manually costs a lot of time and then there's also the chance that this data needs to get altered later in the development process when parts of the application change or get more advanced.

And therefor the factories are also used a lot in tests. While you can add all the data manually by creating it within that test, it will take much more time to write these tests. You propably already have experienced that using a factory is much more efficient.

1 like
kiwi0134's avatar

@gych This is all true and while using Laravel as a framework for the past 8 years, I also only used them for tests.

But this exactly let me question: Are there other valid use cases for factories, or is it considered bad practice? I haven't really found anything related to other use-cases than testing, unfortunately.

My thought on "it's not inside the Tests folder" was: Well, if it's for tests, why can't test dependencies within the tests folder? The TestCase class shipped with the Laravel skeleton is also technically not a test (though it expands on the PHPunit TestCase), but a dependency for other tests.

I hope what I'm trying to say makes any sense.

gych's avatar

@kiwi0134I definitely get your point but the factories are not only designed for the tests. They can also be used to seed your database with thousands of records to fill your application with a lot of data.

Therefor putting the factories in tests folder would be confusing because its also used for db seeding.

https://laravel.com/docs/10.x/seeding

jaseofspades88's avatar

Depends entirely on your use case. If you're creating a new model in your application and you know that any of the random or default fields in the factory apply to a new model you need, then why not use a factory?

If you're confident the factory will give you what you or your users need each time, then roll those dice. I wouldn't recommend it in case of changes to the model factory over time.

kiwi0134's avatar

@jaseofspades88 This is exactly what I meant. Do you have any experience with that use-case? Also regarding caveats when using those factories inside tests, too?

I can imagine that this might be error prone, when it isn't documented inside the project properly, as (new) contributors might not being used to it and simply create / refactor factories for random fake data.

jaseofspades88's avatar
Level 51

It's simply a cleaner api for building out testing models and shouldn't be used for anything more, @kiwi0134. It isn't coupled to the tests directory because they're not intrinsically tests. Continue using them for their intended purpose.

Please or to participate in this conversation.