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

[5.4] Add the lazy method to replace the closure use in model factories #18823

Merged
merged 1 commit into from
Apr 17, 2017

Conversation

sileence
Copy link
Contributor

@sileence sileence commented Apr 17, 2017

Following: #18499

I added the lazy method to delay the creation of models in the database in a more eloquent way:

$factory->define(App\Post::class, function (Faker\Generator $faker) {
    return [
        'user_id' => function() {
            return factory(App\User::class)->create();
        }
   ];
});
$factory->define(App\Post::class, function (Faker\Generator $faker) {
    return [
        'user_id' => factory(App\User::class)->lazy()
   ];
});

What do you think?

@tillkruss tillkruss changed the title Add the lazy method to replace the closure use in model factories [5.4] Add the lazy method to replace the closure use in model factories Apr 17, 2017
@taylorotwell
Copy link
Member

Should I have to call ->create() after ->lazy()... would it be preferable to just say:

'user_id' => factory(App\User::class)->lazy(array $attributes),

@sileence
Copy link
Contributor Author

@taylorotwell done! I thought about it after I submitted the PR 😅

@taylorotwell taylorotwell merged commit 110e8d7 into laravel:5.4 Apr 17, 2017
Copy link
Contributor

@gocanto gocanto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I know what is the beneffit of returning a clause?

@JosephSilber
Copy link
Member

@gocanto
Copy link
Contributor

gocanto commented Apr 24, 2017

Yes @JosephSilber, I read your tweet as soon as it was posted. But, my question is related to the fact of returning a clausure. Why is there a reason for it? Pretty sure this is nor the place to ask this, but if it can be answered it would be nice.

@JosephSilber
Copy link
Member

JosephSilber commented Apr 24, 2017 via email

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.

4 participants