[5.3] Allow additional values to be passed to firstOrCreate #13236

Merged
merged 1 commit into from Apr 21, 2016

Projects

None yet

2 participants

@JosephSilber
Contributor
JosephSilber commented Apr 20, 2016 edited

Many times you want to use some attributes to query, but if you're creating it now you want additional attributes to be saved.


For example, imagine you're using Github OAuth for sign-in. You'd want to use the Github ID to check if the user exists, but if you're creating that user now you'd also want to save their avatar.

Before, you'd do this:

$user = User::firstOrNew(['github_id', $githubUser->id]);

if (! $user->exists) {
    $user->fill(['avatar' => $githubUser->avatar])->save();
}

return $user;

After this change, you can do it all with the firstOrCreate method:

return User::firstOrCreate(['github_id', $githubUser->id], ['avatar' => $githubUser->avatar]);

This PR also adds integration tests for firstOrCreate, which were missing before.

@JosephSilber JosephSilber Allow additional values to be passed to firstOrCreate
ee64cbe
@JosephSilber JosephSilber changed the title from Allow additional values to be passed to firstOrCreate to [5.3] Allow additional values to be passed to firstOrCreate Apr 20, 2016
@taylorotwell taylorotwell merged commit 16a5fb3 into laravel:master Apr 21, 2016

2 checks passed

StyleCI The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@JosephSilber JosephSilber deleted the JosephSilber:additional-uoc-attributes branch May 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment