Skip to content

Commit 1de358c

Browse files
Move updateOrCreate from upsert to update section (#8969)
* Move updateOrCreate from upsert to update section * formatting * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 91a416b commit 1de358c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

eloquent.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,15 @@ The `save` method may also be used to update models that already exist in the da
729729

730730
$flight->save();
731731

732+
Occasionally, you may need to update an existing model or create a new model if no matching model exists. Like the `firstOrCreate` method, the `updateOrCreate` method persists the model, so there's no need to manually call the `save` method.
733+
734+
In the example below, if a flight exists with a `departure` location of `Oakland` and a `destination` location of `San Diego`, its `price` and `discounted` columns will be updated. If no such flight exists, a new flight will be created which has the attributes resulting from merging the first argument array with the second argument array:
735+
736+
$flight = Flight::updateOrCreate(
737+
['departure' => 'Oakland', 'destination' => 'San Diego'],
738+
['price' => 99, 'discounted' => 1]
739+
);
740+
732741
<a name="mass-updates"></a>
733742
#### Mass Updates
734743

@@ -893,21 +902,12 @@ If you wish, you may instruct Laravel to throw an exception when attempting to f
893902
<a name="upserts"></a>
894903
### Upserts
895904

896-
Occasionally, you may need to update an existing model or create a new model if no matching model exists. Like the `firstOrCreate` method, the `updateOrCreate` method persists the model, so there's no need to manually call the `save` method.
897-
898-
In the example below, if a flight exists with a `departure` location of `Oakland` and a `destination` location of `San Diego`, its `price` and `discounted` columns will be updated. If no such flight exists, a new flight will be created which has the attributes resulting from merging the first argument array with the second argument array:
899-
900-
$flight = Flight::updateOrCreate(
901-
['departure' => 'Oakland', 'destination' => 'San Diego'],
902-
['price' => 99, 'discounted' => 1]
903-
);
904-
905-
If you would like to perform multiple "upserts" in a single query, then you should use the `upsert` method instead. The method's first argument consists of the values to insert or update, while the second argument lists the column(s) that uniquely identify records within the associated table. The method's third and final argument is an array of the columns that should be updated if a matching record already exists in the database. The `upsert` method will automatically set the `created_at` and `updated_at` timestamps if timestamps are enabled on the model:
905+
Eloquent's `upsert` method may be used to update or create records in a single, atomic operation. The method's first argument consists of the values to insert or update, while the second argument lists the column(s) that uniquely identify records within the associated table. The method's third and final argument is an array of the columns that should be updated if a matching record already exists in the database. The `upsert` method will automatically set the `created_at` and `updated_at` timestamps if timestamps are enabled on the model:
906906

907907
Flight::upsert([
908908
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
909909
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
910-
], ['departure', 'destination'], ['price']);
910+
], uniqueBy: ['departure', 'destination'], update: ['price']);
911911

912912
> **Warning**
913913
> All databases except SQL Server require the columns in the second argument of the `upsert` method to have a "primary" or "unique" index. In addition, the MySQL database driver ignores the second argument of the `upsert` method and always uses the "primary" and "unique" indexes of the table to detect existing records.

0 commit comments

Comments
 (0)