Skip to content

Related factories in a factory definition, will always be created, despite the use of for() method #58835

@kirkbushell

Description

@kirkbushell

Laravel Version

PHP Version

Database Driver & Version

n/a

Description

When using model factories, whom also create related factories, if you use the for() method when creating the initial model, it will still create the related factories, meaning you end up with 1+ more relationships for that related factory, which is unintuitive. If the for() method is used OR the field is defined for that relationship, the related factory should not be created.

This ruined me for a couple of days trying to figure out why I had multiple related models being setup when I had specified the relationships that I wanted to create, manually for a given model.

Steps To Reproduce

Setup a model with a relationship. Ensure that relationship is also setup as part of the model's factory. Now, set that relationship manually using the for() method. You'll find that 2 relationships are actually created when the test is run.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions