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.3] Allow additional values to be passed to firstOrCreate #13236

merged 1 commit into from Apr 21, 2016


Copy link

JosephSilber commented Apr 20, 2016

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 changed the title Allow additional values to be passed to firstOrCreate [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
2 checks passed
StyleCI The StyleCI analysis has passed
continuous-integration/travis-ci/pr The Travis CI build passed
@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
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.