Skip to content

Commit bc9796a

Browse files
author
Chris Cho
committed
PRR fixes
1 parent 5b5215d commit bc9796a

File tree

2 files changed

+56
-40
lines changed

2 files changed

+56
-40
lines changed

docs/eloquent-models/model-class.txt

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
.. _laravel-eloquent-model-class:
2-
3-
====================
4-
Eloquent Model Class
5-
====================
61

72
.. facet::
83
:name: genre
@@ -42,19 +37,31 @@ Define an Eloquent Model Class
4237
Eloquent models are classes that represent your data. They include methods
4338
that perform database operations such as inserts, updates, and deletes.
4439

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:
4943

5044
.. literalinclude:: /includes/eloquent-models/Planet.php
5145
:language: php
46+
:emphasize-lines: 3,5,7
5247
:dedent:
5348

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+
5865

5966
.. _laravel-authenticatable-model:
6067

@@ -92,13 +99,15 @@ customizations:
9299
Change the Model Collection Name
93100
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
94101

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
97105
class.
98106

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.
102111

103112
The following example specifies the custom MongoDB collection name,
104113
``celestial_body``, for the ``Planet`` class:
@@ -108,6 +117,10 @@ The following example specifies the custom MongoDB collection name,
108117
:emphasize-lines: 9
109118
:dedent:
110119

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+
111124
.. _laravel-model-customize-primary-key:
112125

113126
Change the Primary Key Field
@@ -116,8 +129,8 @@ Change the Primary Key Field
116129
To customize the model's primary key field that uniquely identify a MongoDB
117130
document, override the ``$primaryKey`` property of the model class.
118131

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.
121134

122135
The following example specifies the ``name`` field as the primary key for
123136
the ``Planet`` class:
@@ -131,8 +144,8 @@ To learn more about primary key behavior and customization options, see
131144
`Eloquent Primary Keys <https://laravel.com/docs/{+laravel-docs-version+}/eloquent#primary-keys>`__
132145
in the Laravel docs.
133146

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.
136149

137150
.. _laravel-model-soft-delete:
138151

@@ -142,12 +155,11 @@ Enable Soft Deletes
142155
Eloquent includes a soft delete feature that changes the behavior of the
143156
``delete()`` method on a model. When soft delete is enabled on a model, the
144157
``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
146159
retrieve operations automatically.
147160

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:
151163

152164
.. literalinclude:: /includes/eloquent-models/PlanetSoftDelete.php
153165
:language: php
@@ -206,8 +218,8 @@ an array of data to the ``create()`` model method. This process of inserting
206218
multiple models is called mass assignment.
207219

208220
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.
211223

212224
Eloquent provides the following traits to protect your data from mass
213225
assignment vulnerabilities:
@@ -227,7 +239,7 @@ by using the ``$fillable`` attribute:
227239

228240
.. literalinclude:: /includes/eloquent-models/PlanetMassAssignment.php
229241
:language: php
230-
:emphasize-lines: 14-19
242+
:emphasize-lines: 9-14
231243
:dedent:
232244

233245
The following code example shows mass assignment of the ``Planet`` model:
@@ -254,7 +266,7 @@ Specify Pruning Behavior
254266
------------------------
255267

256268
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,
258270
Laravel calls the ``prunable()`` method on all models that import the
259271
``Prunable`` and ``MassPrunable`` traits to match the models for deletion.
260272

@@ -266,15 +278,24 @@ appropriate import to your model:
266278
- ``MongoDB\Laravel\Eloquent\MassPrunable`` deletes models that match the
267279
criteria without fetching the model data
268280

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+
269290
To learn more about the pruning feature, see `Pruning Models <https://laravel.com/docs/{+laravel-docs-version+}/eloquent#pruning-models>`__
270291
in the Laravel docs.
271292

272293
Prunable Example
273294
~~~~~~~~~~~~~~~~
274295

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:
278299

279300
.. literalinclude:: /includes/eloquent-models/PlanetPrune.php
280301
:language: php
@@ -283,8 +304,8 @@ The following example model shows how to perform a prune that calls the
283304
Mass Prunable Example
284305
~~~~~~~~~~~~~~~~~~~~~
285306

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:
288309

289310
.. literalinclude:: /includes/eloquent-models/PlanetMassPrune.php
290311
:language: php

docs/includes/eloquent-models/PlanetMassAssignment.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66

77
class Planet extends Model
88
{
9-
/**
10-
* The mass assignable attributes.
11-
*
12-
* @var array
13-
*/
149
protected $fillable = [
1510
'name',
1611
'gravitational_force',

0 commit comments

Comments
 (0)