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

[8.x] Document the new upsert method #6466

Merged
merged 1 commit into from Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions eloquent.md
Expand Up @@ -609,6 +609,15 @@ You may also come across situations where you want to update an existing model o
['price' => 99, 'discounted' => 1]
);

If you would like to perform multiple "upserts", then you should use the `upsert` method instead. The first argument is the values to insert or update, and the second argument is the column(s) that uniquely identify records.

$numRecords = App\Models\Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination']);

The `upsert` method will also automatically set the `updated_at` timestamps, if timestamps are enabled on the model.

<a name="deleting-models"></a>
## Deleting Models

Expand Down
7 changes: 7 additions & 0 deletions queries.md
Expand Up @@ -694,6 +694,13 @@ The `insertOrIgnore` method will ignore duplicate record errors while inserting
['id' => 2, 'email' => 'dayle@example.com'],
]);

The `upsert` method will insert rows that do not exist and update the rows that already exist with the new values. The first argument is the values to insert or update, and the second argument is the column(s) that uniquely identify records.

DB::table('users')->upsert([
['email' => 'taylor@example.com', 'votes' => 0],
['email' => 'dayle@example.com', 'votes' => 0],
], 'email');

#### Auto-Incrementing IDs

If the table has an auto-incrementing id, use the `insertGetId` method to insert a record and then retrieve the ID:
Expand Down