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] Add ->set() method to factory #41890

Merged
merged 3 commits into from
Apr 8, 2022
Merged

Conversation

ralphjsmit
Copy link
Contributor

@ralphjsmit ralphjsmit commented Apr 8, 2022

Lately I've been writing unit-tests for the models of an application that did not have many tests yet. In that application many scopes were used.

I noticed that I was often creating models like this, where you just need to make one little change every time:

EloquentModel::factory()
    ->create(['name' => 'foo']);

EloquentModel::factory()
    ->someMethod()
    ->create(['country' => 'NL']);

I figured out that the tests would look a lot cleaner if we could just get rid of those single-item arrays, so I came up with a small method called ->set(). This method is in my eyes easier and more expressive than ->create(['foo' => 'bar']), because it is immediately clear that we're setting only a single property here.

EloquentModel::factory()
    ->set('name', 'foo')
    ->create();

EloquentModel::factory()
    ->someMethod()
    ->set('country', 'NL')
    ->create();

Another option would be to name the method ->put(), in line with the similar collection method (but personally I prefer ->set()).

@taylorotwell taylorotwell merged commit ce3d25c into laravel:9.x Apr 8, 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.

None yet

2 participants