Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Factory fails to eval models and factories when returned from closure #42344

Merged
merged 2 commits into from
May 11, 2022
Merged

[9.x] Factory fails to eval models and factories when returned from closure #42344

merged 2 commits into from
May 11, 2022

Conversation

harrygulliford
Copy link
Contributor

@harrygulliford harrygulliford commented May 11, 2022

Since v9.12.0, due to the changes made in #42241, model factories now fail to evaluate both models and factories when they are returned from a closure.

This is due to models and factories now being resolved first and closures second.

To fix this issue, while maintaining the functionality added in #42241, the resolution of models and factories would need to be repeated again after resolving closures.

A rudimentary example:

class PostFactory extends Factory
{
    public function definition()
    {
        return [
            'title' => 'post',
            'user_id' => function (array $attributes) {
                return User::factory(['options' => $attributes['title'].'-options']);
            },
        ];
    }
}

In the latest release, the code above would throw an exception - occurring when the database driver attempts to bind a factory object the query statement (see failing test).

Error: Object of class \Database\Factories\PostFactory could not be converted to string

@harrygulliford harrygulliford changed the title [9.12.0] Factory fails to eval models and factories when returned from closure [9.x] Factory fails to eval models and factories when returned from closure May 11, 2022
@taylorotwell taylorotwell merged commit 4ebd79d into laravel:9.x May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants