1
- .. _laravel-eloquent-model-class:
2
-
3
- ====================
4
- Eloquent Model Class
5
- ====================
6
1
7
2
.. facet::
8
3
:name: genre
@@ -42,19 +37,31 @@ Define an Eloquent Model Class
42
37
Eloquent models are classes that represent your data. They include methods
43
38
that perform database operations such as inserts, updates, and deletes.
44
39
45
- To declare a {+odm-short+} model class, create a file in your Laravel
46
- application ``app/Models`` directory. Add the import for the
47
- ``MongoDB\Laravel\Eloquent\Model`` package and extend the ``Model`` class as
48
- shown in the following code example:
40
+ To declare a {+odm-short+} model, create a class in the ``app/Models``
41
+ directory of your Laravel application that extends
42
+ ``MongoDB\Laravel\Eloquent\Model`` as shown in the following code example:
49
43
50
44
.. literalinclude:: /includes/eloquent-models/Planet.php
51
45
:language: php
46
+ :emphasize-lines: 3,5,7
52
47
:dedent:
53
48
54
- Alternatively, use the ``artisan`` console to generate the model class and
55
- change the ``Illuminate\Database\Eloquent\Model`` import to ``MongoDB\Laravel\Eloquent\Model``.
56
- To learn more about the ``artisan`` console, see `Artisan Console <https://laravel.com/docs/{+laravel-docs-version+}/artisan>`__
57
- in the Laravel docs.
49
+ By default, the model uses the MongoDB database name set in your Laravel
50
+ application's ``config/database.php`` setting and the snake case plural
51
+ form of your model class name for the collection.
52
+
53
+ This model is stored in the ``planets`` MongoDB collection.
54
+
55
+ .. tip::
56
+
57
+ Alternatively, use the ``artisan`` console to generate the model class and
58
+ change the ``Illuminate\Database\Eloquent\Model`` import to ``MongoDB\Laravel\Eloquent\Model``.
59
+ To learn more about the ``artisan`` console, see `Artisan Console <https://laravel.com/docs/{+laravel-docs-version+}/artisan>`__
60
+ in the Laravel docs.
61
+
62
+ To learn how to specify the database name that your Laravel application uses,
63
+ :ref:`laravel-quick-start-connect-to-mongodb`.
64
+
58
65
59
66
.. _laravel-authenticatable-model:
60
67
@@ -92,13 +99,15 @@ customizations:
92
99
Change the Model Collection Name
93
100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94
101
95
- To customize the name of the collection the model uses to retrieve and save
96
- data in MongoDB, override the ``$collection`` property of the model
102
+ By default, the model uses the snake case plural form of your model
103
+ class name. To change the name of the collection the model uses to retrieve
104
+ and save data in MongoDB, override the ``$collection`` property of the model
97
105
class.
98
106
99
- By default, the model uses the MongoDB database name set in your Laravel
100
- application's ``config/database.php`` setting and the snake case plural form of
101
- your model class name for the collection.
107
+ .. note::
108
+
109
+ We recommend using the default collection naming behavior to keep
110
+ the associations between models and collections straightforward.
102
111
103
112
The following example specifies the custom MongoDB collection name,
104
113
``celestial_body``, for the ``Planet`` class:
@@ -108,6 +117,10 @@ The following example specifies the custom MongoDB collection name,
108
117
:emphasize-lines: 9
109
118
:dedent:
110
119
120
+ Without overriding the ``$collection`` property, this model maps to the
121
+ ``planets`` collection. With the overridden property, the example class stores
122
+ the model in the ``celestial_body`` collection.
123
+
111
124
.. _laravel-model-customize-primary-key:
112
125
113
126
Change the Primary Key Field
@@ -116,8 +129,8 @@ Change the Primary Key Field
116
129
To customize the model's primary key field that uniquely identify a MongoDB
117
130
document, override the ``$primaryKey`` property of the model class.
118
131
119
- By default, the model uses the PHP MongoDB driver to generate unique IDs for
120
- each document your Laravel application inserts.
132
+ By default, the model uses the PHP MongoDB driver to generate unique ObjectIDs
133
+ for each document your Laravel application inserts.
121
134
122
135
The following example specifies the ``name`` field as the primary key for
123
136
the ``Planet`` class:
@@ -131,8 +144,8 @@ To learn more about primary key behavior and customization options, see
131
144
`Eloquent Primary Keys <https://laravel.com/docs/{+laravel-docs-version+}/eloquent#primary-keys>`__
132
145
in the Laravel docs.
133
146
134
- To learn more about the ``_id`` field and the MongoDB document structure, see
135
- :manual:`Documents </core/document>` in the MongoDB server docs.
147
+ To learn more about the ``_id`` field, ObjectIDs, and the MongoDB document
148
+ structure, see :manual:`Documents </core/document>` in the MongoDB server docs.
136
149
137
150
.. _laravel-model-soft-delete:
138
151
@@ -142,12 +155,11 @@ Enable Soft Deletes
142
155
Eloquent includes a soft delete feature that changes the behavior of the
143
156
``delete()`` method on a model. When soft delete is enabled on a model, the
144
157
``delete()`` method marks a document as deleted instead of removing it from the
145
- database. It sets a timestamp on the ``deleted_at`` field to exclude it from
158
+ database. It sets a timestamp on the ``deleted_at`` field to exclude it from
146
159
retrieve operations automatically.
147
160
148
- To enable soft deletes on a class, import the ``MongoDB\Laravel\Eloquent\SoftDeletes``
149
- package and add the trait to the class definition as shown in the following
150
- code example:
161
+ To enable soft deletes on a class, add the ``MongoDB\Laravel\Eloquent\SoftDeletes``
162
+ trait as shown in the following code example:
151
163
152
164
.. literalinclude:: /includes/eloquent-models/PlanetSoftDelete.php
153
165
:language: php
@@ -206,8 +218,8 @@ an array of data to the ``create()`` model method. This process of inserting
206
218
multiple models is called mass assignment.
207
219
208
220
Mass assignment can be an efficient way to create multiple models. However, it
209
- can be expose an exploitable security vulnerability. The data in the fields
210
- could contain updates that could lead to unauthorized permissions or access.
221
+ can expose an exploitable security vulnerability. The data in the fields
222
+ might contain updates that lead to unauthorized permissions or access.
211
223
212
224
Eloquent provides the following traits to protect your data from mass
213
225
assignment vulnerabilities:
@@ -227,7 +239,7 @@ by using the ``$fillable`` attribute:
227
239
228
240
.. literalinclude:: /includes/eloquent-models/PlanetMassAssignment.php
229
241
:language: php
230
- :emphasize-lines: 14-19
242
+ :emphasize-lines: 9-14
231
243
:dedent:
232
244
233
245
The following code example shows mass assignment of the ``Planet`` model:
@@ -254,7 +266,7 @@ Specify Pruning Behavior
254
266
------------------------
255
267
256
268
Eloquent lets you specify criteria to periodically delete model data that you
257
- no longer need. When you schedule or run the ``model:prune`` command,
269
+ no longer need. When you schedule or run the ``model:prune`` command,
258
270
Laravel calls the ``prunable()`` method on all models that import the
259
271
``Prunable`` and ``MassPrunable`` traits to match the models for deletion.
260
272
@@ -266,15 +278,24 @@ appropriate import to your model:
266
278
- ``MongoDB\Laravel\Eloquent\MassPrunable`` deletes models that match the
267
279
criteria without fetching the model data
268
280
281
+ .. note::
282
+
283
+ When enabling soft deletes on a mass prunable model, you must import both
284
+ of the following {+odm-short+} packages:
285
+
286
+ - ``MongoDB\Laravel\Eloquent\SoftDeletes``
287
+ - ``MongoDB\Laravel\Eloquent\MassPrunable``
288
+
289
+
269
290
To learn more about the pruning feature, see `Pruning Models <https://laravel.com/docs/{+laravel-docs-version+}/eloquent#pruning-models>`__
270
291
in the Laravel docs.
271
292
272
293
Prunable Example
273
294
~~~~~~~~~~~~~~~~
274
295
275
- The following example model shows how to perform a prune that calls the
276
- ``pruning()`` method before deleting a model that matches the query in the
277
- ``prunable()`` method :
296
+ The following prunable class includes a ``prunable()`` method that matches
297
+ models that the prune action deletes and a ``pruning()`` method that runs
298
+ before deleting a matching model :
278
299
279
300
.. literalinclude:: /includes/eloquent-models/PlanetPrune.php
280
301
:language: php
@@ -283,8 +304,8 @@ The following example model shows how to perform a prune that calls the
283
304
Mass Prunable Example
284
305
~~~~~~~~~~~~~~~~~~~~~
285
306
286
- The following example model shows how to perform a "mass prune" which deletes
287
- all models that match the query in the ``prunable()`` method :
307
+ The following mass prunable class includes a ``prunable()`` method that matches
308
+ models that the prune action deletes :
288
309
289
310
.. literalinclude:: /includes/eloquent-models/PlanetMassPrune.php
290
311
:language: php
0 commit comments