Update Multiple Documents
On this page
You can update multiple documents in a collection by calling the update()
method
on a query builder.
Pass a query filter to the where()
method to retrieve documents that meet a
set of criteria. Then, update the matching documents by passing your intended
document changes to the update()
method.
Example
This usage example performs the following actions:
Uses the
Movie
Eloquent model to represent themovies
collection in thesample_mflix
databaseUpdates documents from the
movies
collection that match a query filterPrints the number of updated documents
The example calls the following methods on the Movie
model:
where()
: matches documents in which the value of theimdb.rating
nested field is greater than9.0
.update()
: updates the matching documents by adding anacclaimed
field and setting its value totrue
. This method returns the number of documents that were successfully updated.
$updates = Movie::where('imdb.rating', '>', 9.0) ->update(['acclaimed' => true]); echo 'Updated documents: ' . $updates;
Updated documents: 20
To learn how to edit your Laravel application to run the usage example, see the Usage Examples landing page.
Tip
To learn more about updating data with the Laravel Integration, see the Modify Documents section of the Write Operations guide.