Laravel - Getting Started - Upserts
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.
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:
$flight = Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1]
);
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:
Flight::upsert([
['departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],
['departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]
], ['departure', 'destination'], ['price']);