Skip to content

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

Merged
taylorotwell merged 1 commit into
laravel:5.4from
sileence:factory_lazy
Apr 17, 2017
Merged

[5.4] Add the lazy method to replace the closure use in model factories#18823
taylorotwell merged 1 commit into
laravel:5.4from
sileence:factory_lazy

Conversation

@sileence

@sileence sileence commented Apr 17, 2017

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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

@gocanto gocanto left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

@gocanto

gocanto commented Apr 24, 2017

Copy link
Copy Markdown
Contributor

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

JosephSilber commented Apr 24, 2017 via email

Copy link
Copy Markdown
Contributor

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