@@ -9,7 +9,7 @@ Write Operations
9
9
:values: tutorial
10
10
11
11
.. meta::
12
- :keywords: insert, insert one, update, update one, upsert, delete, delete many, code example, mass assignment, eloquent model
12
+ :keywords: insert, insert one, code example, mass assignment, eloquent model
13
13
14
14
.. contents:: On this page
15
15
:local:
@@ -27,28 +27,15 @@ inserting, updating, and deleting data based on specified criteria.
27
27
This guide shows you how to perform the following tasks:
28
28
29
29
- :ref:`laravel-fundamentals-insert-documents`
30
- - Modify Documents
30
+ - :ref:`laravel-fundamentals-modify-documents`
31
31
- Delete Documents
32
32
33
- .. _laravel-fundamentals-insert-documents:
34
-
35
- Insert Documents
36
- ----------------
37
-
38
- In this section, you can learn how to insert documents into MongoDB collections
39
- from your Laravel application by using the {+odm-long+}.
40
-
41
- When you insert the documents, ensure the data does not violate any
42
- unique indexes on the collection. When inserting the first document of a
43
- collection or creating a new collection, MongoDB automatically creates a
44
- unique index on the ``_id`` field.
33
+ .. _laravel-fundamentals-write-sample-model:
45
34
46
- For more information on creating indexes on MongoDB collections by using the
47
- Laravel schema builder, see the :ref:`laravel-eloquent-indexes` section
48
- of the Schema Builder documentation.
35
+ Sample Model
36
+ ~~~~~~~~~~~~
49
37
50
- This section uses the following example model class to demonstrate how to
51
- use Eloquent models to perform insert operations:
38
+ The write operations in this guide reference the following Eloquent model class:
52
39
53
40
.. literalinclude:: /includes/fundamentals/write-operations/Concert.php
54
41
:language: php
@@ -65,6 +52,23 @@ use Eloquent models to perform insert operations:
65
52
data types. To learn more, see `Attribute Casting <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-mutators#attribute-casting>`__
66
53
in the Laravel documentation.
67
54
55
+ .. _laravel-fundamentals-insert-documents:
56
+
57
+ Insert Documents
58
+ ----------------
59
+
60
+ In this section, you can learn how to insert documents into MongoDB collections
61
+ from your Laravel application by using the {+odm-long+}.
62
+
63
+ When you insert the documents, ensure the data does not violate any
64
+ unique indexes on the collection. When inserting the first document of a
65
+ collection or creating a new collection, MongoDB automatically creates a
66
+ unique index on the ``_id`` field.
67
+
68
+ For more information on creating indexes on MongoDB collections by using the
69
+ Laravel schema builder, see the :ref:`laravel-eloquent-indexes` section
70
+ of the Schema Builder documentation.
71
+
68
72
To learn more about Eloquent models in {+odm-short+}, see the :ref:`laravel-eloquent-models`
69
73
section.
70
74
@@ -75,13 +79,16 @@ These examples show how to use the ``save()`` Eloquent method to insert an
75
79
instance of a ``Concert`` model as a MongoDB document.
76
80
77
81
When the ``save()`` method succeeds, you can access the model instance on
78
- which you called the method. If the operation fails, the model instance is
79
- assigned ``null``.
82
+ which you called the method.
83
+
84
+ If the operation fails, the model instance is assigned ``null``.
80
85
81
86
This example code performs the following actions:
82
87
83
88
- Creates a new instance of the ``Concert`` model
84
89
- Assigns string values to the ``performer`` and ``venue`` fields
90
+ - Assigns an array of strings to the ``genre`` field
91
+ - Assigns a number to the ``ticketsSold`` field
85
92
- Assigns a date to the ``performanceDate`` field by using the ``Carbon``
86
93
package
87
94
- Inserts the document by calling the ``save()`` method
@@ -93,7 +100,7 @@ This example code performs the following actions:
93
100
:end-before: end model insert one
94
101
95
102
You can retrieve the inserted document's ``_id`` value by accessing the model's
96
- ``id`` member as shown in the following code example:
103
+ ``id`` member, as shown in the following code example:
97
104
98
105
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
99
106
:language: php
@@ -103,7 +110,7 @@ You can retrieve the inserted document's ``_id`` value by accessing the model's
103
110
104
111
If you enable mass assignment by defining either the ``$fillable`` or
105
112
``$guarded`` attributes, you can use the Eloquent model ``create()`` method
106
- to perform the insert in a single call as shown in the following example:
113
+ to perform the insert in a single call, as shown in the following example:
107
114
108
115
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
109
116
:language: php
@@ -122,21 +129,122 @@ multiple instances of a ``Concert`` model as MongoDB documents. This bulk
122
129
insert method reduces the number of calls your application needs to make
123
130
to save the documents.
124
131
125
- When the ``insert()`` method succeeds, it returns the value ``1``. If it
126
- fails, it throws an exception.
132
+
133
+ When the ``insert()`` method succeeds, it returns the value ``1``.
134
+
135
+ If it fails, it throws an exception.
127
136
128
137
The example code saves multiple models in a single call by passing them as
129
138
an array to the ``insert()`` method:
130
139
131
140
.. note::
132
141
133
- This example wraps the dates in the `MongoDB\BSON\UTCDateTime <{+phplib-api+}/class.mongodb-bson-utcdatetime.php>`__
134
- class to convert it to a type MongoDB can serialize because Laravel
135
- skips attribute casting on bulk insert operations.
142
+ This example wraps the dates in the `MongoDB\\ BSON\ \UTCDateTime <{+phplib-api+}/class.mongodb-bson-utcdatetime.php>`__
143
+ class to convert it to a type MongoDB can serialize because Laravel
144
+ skips attribute casting on bulk insert operations.
136
145
137
146
.. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
138
147
:language: php
139
148
:dedent:
140
149
:start-after: begin model insert many
141
150
:end-before: end model insert many
142
151
152
+ .. _laravel-fundamentals-modify-documents:
153
+
154
+ Modify Documents
155
+ ----------------
156
+
157
+ In this section, you can learn how to modify documents in your MongoDB
158
+ collection from your Laravel application. Use update operations to modify
159
+ existing documents or to insert a document if none match the search criteria.
160
+
161
+ You can persist changes on an instance of an Eloquent model or use
162
+ Eloquent's fluent syntax to chain an update operation on methods that
163
+ return a Laravel collection object.
164
+
165
+ This section provides examples of the following update operations:
166
+
167
+ - :ref:`Update a document <laravel-modify-documents-update-one>`
168
+ - :ref:`Update multiple documents <laravel-modify-documents-update-multiple>`
169
+
170
+ .. _laravel-modify-documents-update-one:
171
+
172
+ Update a Document Examples
173
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
174
+
175
+ You can update a document in the following ways:
176
+
177
+ - Modify an instance of the model and save the changes by calling the ``save()``
178
+ method.
179
+ - Chain methods to retrieve an instance of a model and perform updates on it
180
+ by calling the ``update()`` method.
181
+
182
+ The following example shows how to update a document by modifying an instance
183
+ of the model and calling its ``save()`` method:
184
+
185
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
186
+ :language: php
187
+ :dedent:
188
+ :start-after: begin model update one save
189
+ :end-before: end model update one save
190
+
191
+ When the ``save()`` method succeeds, the model instance on which you called the
192
+ method contains the updated values.
193
+
194
+ If the operation fails, {+odm-short+} assigns the model instance a null value.
195
+
196
+ The following example shows how to update a document by chaining methods to
197
+ retrieve and update the first matching document:
198
+
199
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
200
+ :language: php
201
+ :dedent:
202
+ :start-after: begin model update one fluent
203
+ :end-before: end model update one fluent
204
+
205
+ .. note::
206
+
207
+ The ``orderBy()`` call sorts the results by the ``_id`` field to
208
+ guarantee a consistent sort order. To learn more about sorting in MongoDB,
209
+ see the :manual:`Natural order </reference/glossary/#std-term-natural-order>`
210
+ glossary entry in the {+server-docs-name+}.
211
+
212
+ When the ``update()`` method succeeds, the operation returns the number of
213
+ documents updated.
214
+
215
+ If the retrieve part of the call does not match any documents, {+odm-short+}
216
+ returns the following error:
217
+
218
+ .. code-block:: none
219
+ :copyable: false
220
+
221
+ Error: Call to a member function update() on null
222
+
223
+ .. _laravel-modify-documents-update-multiple:
224
+
225
+ Update Multiple Documents Example
226
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227
+
228
+ To perform an update on one or more documents, chain the ``update()``
229
+ method to the results of a method that retrieves the documents as a
230
+ Laravel collection object, such as ``where()``.
231
+
232
+ The following example shows how to chain calls to retrieve matching documents
233
+ and update them:
234
+
235
+ .. literalinclude:: /includes/fundamentals/write-operations/WriteOperationsTest.php
236
+ :language: php
237
+ :dedent:
238
+ :start-after: begin model update multiple
239
+ :end-before: end model update multiple
240
+
241
+ When the ``update()`` method succeeds, the operation returns the number of
242
+ documents updated.
243
+
244
+ If the retrieve part of the call does not match any documents in MongoDB,
245
+ {+odm-short+} returns the following error:
246
+
247
+ .. code-block:: none
248
+ :copyable: false
249
+
250
+ Error: Call to a member function update() on null
0 commit comments