[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

Projects
None yet
4 participants
@sileence
Contributor

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 from Add the lazy method to replace the closure use in model factories to [5.4] Add the lazy method to replace the closure use in model factories Apr 17, 2017

@taylorotwell

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Apr 17, 2017

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),
Member

taylorotwell commented Apr 17, 2017

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

This comment has been minimized.

Show comment
Hide comment
@sileence

sileence Apr 17, 2017

Contributor

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

Contributor

sileence commented Apr 17, 2017

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

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

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@gocanto

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

@gocanto

This comment has been minimized.

Show comment
Hide comment
@gocanto

gocanto Apr 24, 2017

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.

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

This comment has been minimized.

Show comment
Hide comment
@JosephSilber

JosephSilber Apr 24, 2017

Contributor
Contributor

JosephSilber commented Apr 24, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment