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

stevemoretz's avatar

Factory a way to ignore definition

I love Laravel factories, I use them in tests and that's why my definition includes some faker arguments eg:

    public function definition()
    {
        return [
            "post_title" => fake()->title(),
            "post_content" => fake()->randomHtml(),
            "post_excerpt" => fake()->paragraph(1),
        ];
    }

But I also like to use factories inside my controllers, (I don't use factories with eloquent I have some custom implementation on them so it does make sense to use in it controllers too).

The only issue is what if I don't want to use post_excerpt for instance? if I don't provide it in will use fake()->paragraph(1) which is obviously not what I need, is there any way that you can ignore the definition altogether while using a factory, or just remove a few fields from it?

I know I can just add post_excerpt part with a state but that means I'll have a lot more states just to make this work, another way might be that I provide null as an override, but I'm looking for a cleaner way. (if it does exist)

0 likes
7 replies
cwhite's avatar
cwhite
Best Answer
Level 19

Any attributes you pass to the factory will override the defaults:

Factory::factory()
    ->create(['post_excerpt' => null]);

Although I would not recommend factories for generating production data....if you need default attributes the use the $attributes property on your eloquent models.

1 like
stevemoretz's avatar

@cwhite Thank you for answering.

May I ask what's wrong with using that in production?

I don't use eloquent as I mentioned I repurposed factories they do something else I just use their attribute and state features which are awesome, then I use that data to do something with, not using eloquent at all.

cwhite's avatar

@stevemoretz,

The intended use for a factory is to quickly populate models with fake data for use in testing(and possibly in your local environment).

I'm not sure how you're using factories (how are you using factories?), but there's probably a better method / design pattern to accomplish your intended use case.

stevemoretz's avatar

@cwhite

I use Laravel on top of Wordpress now wordpress has its own methods for creating its stuff in the DB, I did manage to make factories work with those methods and now I fill WordPress database almost just like the way we do in Laravel (no eloquent anymore though), though since I did a lot of overriding and thinking of this, I was hoping if the performance won't be a problem, just use the factories in the controllers too, as as it makes everything unified and the duplication becomes much less.

cwhite's avatar

@stevemoretz , that's interesting....

Well that's how you override attributes or you can define factory states on the model if necessary. If your question has been fully answered then please mark a Best Answer to close the question :).

stevemoretz's avatar

@cwhite I can define factory states in the factory if necessary, you made me realize I probably shouldn't use factories in the controllers thanks for that too.

syber's avatar

For me, I used this syntax

Factory::factory()->newModel()->created([]);"

When I check insert in my database I have the same problem. Data was created by the definition

Please or to participate in this conversation.