@@ -15,7 +15,7 @@ Overview
1515--------
1616
1717This page describes the following Laravel Eloquent and MongoDB-specific
18- relationships available in {+odm-short+} and shows examples on how to define
18+ relationships available in {+odm-short+} and shows examples of how to define
1919and use them:
2020
2121- :ref:`One to one relationship <laravel-eloquent-relationship-one-to-one>`,
@@ -27,25 +27,23 @@ and use them:
2727- :ref:`Embedded document pattern <laravel-embedded-document-pattern>`, a
2828 MongoDB-specific relationship that can represent a one to one or one to many
2929 relationship, created by using the ``embedsOne()`` or ``embedsMany()`` method
30- - :ref:`Cross-database Relationships <laravel-relationship-cross-database>`,
31- required when you want to define
32- relationships between MongoDB and SQL models.
30+ - :ref:`Cross-database relationships <laravel-relationship-cross-database>`,
31+ required when you want to create relationships between MongoDB and SQL models
3332
34- To establish a relationship, add a function to the model class that calls
35- the appropriate relationship method . This function allows you to access the
36- related model as a **dynamic property**. A dynamic property lets you access
37- the related model by using the same syntax as you use to access a property
38- on the model.
33+ Add a function to the model class that calls the appropriate relationship method
34+ to establish a relationship . This function allows you to access the related
35+ model as a **dynamic property**. A dynamic property lets you access the
36+ related model by using the same syntax as you use to access a property on the
37+ model.
3938
4039.. _laravel-eloquent-relationship-one-to-one:
4140
4241One to One Relationship
4342-----------------------
4443
45- A one to one relationship between models consists of a model record that
46- is related to exactly one other type of model record. In MongoDB, a record
47- is represented as a document and different model types exist in separate
48- collections.
44+ A one to one relationship between models consists of a model record related to
45+ exactly one other type of model record. In MongoDB, a record is represented as
46+ a document, and different model types exist in separate collections.
4947
5048When you add a one to one relationship by using the method, Eloquent lets you
5149access the model by using a dynamic property and stores the model's document
@@ -58,9 +56,6 @@ When you add the inverse of the relationship by using the ``belongsTo()``
5856method, Eloquent lets you access the model by using a dynamic property, but
5957does not add any fields.
6058
61- The following section shows an example of how to create a one to one
62- relationship.
63-
6459To learn more about one to one relationships, see
6560`One to One <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#one-to-one>`__
6661in the Laravel docs.
@@ -83,8 +78,8 @@ as shown in the following example class:
8378 :language: php
8479 :dedent:
8580
86- The following sample code shows how you can instantiate a model for each class
87- and add the relationship between them. Click the :guilabel:`Output` button to
81+ The following sample code shows how to instantiate a model for each class
82+ and add the relationship between them. Click the :guilabel:`View Output` button to
8883see sample MongoDB documents created by running the code:
8984
9085.. io-code-block::
@@ -116,7 +111,7 @@ see sample MongoDB documents created by running the code:
116111 // ...
117112 }
118113
119- The following sample code shows how you can access the related models by using
114+ The following sample code shows how to access the related models by using
120115the dynamic properties as defined in the example classes:
121116
122117.. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
@@ -131,23 +126,20 @@ One to Many Relationship
131126------------------------
132127
133128A one to many relationship between models consists of a model that is
134- the parent and one or more related model records which are the children .
129+ the parent and one or more related child model records.
135130
136131When you add a one to many relationship method, Eloquent lets you access the
137132model by using a dynamic property and stores the parent model's document ID
138133on each of the child model documents.
139134
140135In {+odm-short+}, you can define a one to many relationship by using the
141- ``hasMany()`` method on the parent class and optionally the ``belongsTo()``
136+ ``hasMany()`` method on the parent class and, optionally, the ``belongsTo()``
142137method on the child class.
143138
144139When you add the inverse of the relationship by using the ``belongsTo()``
145140method, Eloquent lets you access the parent model by using a dynamic property
146141without adding any fields.
147142
148- The following section shows an example of how to create a one to many
149- relationship.
150-
151143To learn more about one to many relationships, see
152144`One to Many <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#one-to-many>`__
153145in the Laravel docs.
@@ -163,15 +155,15 @@ relationship between a ``Planet`` parent model and ``Moon`` child model.
163155 :dedent:
164156
165157To define the inverse of the relationship on ``Moon``, add the dynamic
166- property and call the ``belongsTo()`` method on it as shown in the following
158+ property and call the ``belongsTo()`` method on it, as shown in the following
167159example class:
168160
169161.. literalinclude:: /includes/eloquent-models/relationships/MoonOneToMany.php
170162 :language: php
171163 :dedent:
172164
173- The following sample code shows how you can instantiate a model for each class
174- and add the relationship between them. Click the :guilabel:`Output` button to
165+ The following sample code shows how ton instantiate a model for each class
166+ and add the relationship between them. Click the :guilabel:`View Output` button to
175167see sample MongoDB documents created by running the code:
176168
177169.. io-code-block::
@@ -212,7 +204,7 @@ see sample MongoDB documents created by running the code:
212204 }
213205 ]
214206
215- The following sample code shows how you can access the related models by using
207+ The following sample code shows how to access the related models by using
216208the dynamic properties as defined in the example classes.
217209
218210.. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
@@ -236,22 +228,22 @@ In {+odm-short+}, you can define a many to many relationship by adding the
236228
237229When you define a many to many relationship in a relational database, Laravel
238230creates a pivot table to track the relationships. When you use {+odm-short+},
239- it omits the pivot table creation and instead adds the related document IDs
240- to a document field, derived from the related model class name.
231+ it omits the pivot table creation and adds the related document IDs to a
232+ document field, derived from the related model class name.
241233
242234.. tip::
243235
244236 Since {+odm-short+} uses a document field instead of a pivot table, omit
245237 the pivot table parameter from the ``belongsToMany()`` constructor or set
246238 it to ``null``.
247239
248- The following section shows an example of how to create a many to many
249- relationship between model classes.
250-
251240To learn more about many to many relationships in Laravel, see
252241`Many to Many <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#many-to-many>`__
253242in the Laravel docs.
254243
244+ The following section shows an example of how to create a many to many
245+ relationship between model classes.
246+
255247Many to Many Example
256248~~~~~~~~~~~~~~~~~~~~
257249
@@ -269,8 +261,8 @@ relationship with ``Planet`` as shown in the following example class:
269261 :language: php
270262 :dedent:
271263
272- The following sample code shows how you can instantiate a model for each class
273- and add the relationship between them. Click the :guilabel:`Output` button to
264+ The following sample code shows how to instantiate a model for each class
265+ and add the relationship between them. Click the :guilabel:`View Output` button to
274266see sample MongoDB documents created by running the code:
275267
276268.. io-code-block::
@@ -338,7 +330,7 @@ see sample MongoDB documents created by running the code:
338330 }
339331 ]
340332
341- The following sample code shows how you can access the related models by using
333+ The following sample code shows how to access the related models by using
342334the dynamic properties as defined in the example classes.
343335
344336.. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
@@ -357,8 +349,9 @@ In MongoDB, the embedded document pattern adds the related model's data into
357349the parent model instead of keeping foreign key references. This pattern
358350when you must optimize for one or more of the following requirements:
359351
360- - Keep related data together in a single collection
361- - Perform atomic updates on multiple fields of the document and the related data
352+ - Keep associated data together in a single collection
353+ - Perform atomic updates on multiple fields of the document and the associated
354+ data
362355- Reduce the number of reads required to fetch the data
363356
364357In {+odm-short+}, you can define embedded documents by using one of the
@@ -372,15 +365,15 @@ following dynamic property methods:
372365 These methods return Eloquent collections, which differ from query builder
373366 objects.
374367
375- The following section shows an example of how to use the embedded document
376- pattern.
377-
378368To learn more about the MongoDB embedded document pattern, see the following
379369MongoDB server tutorials:
380370
381371- :manual:`Model One-to-One Relationships with Embedded Documents </tutorial/model-embedded-one-to-one-relationships-between-documents/>`
382372- :manual:`Model One-to-Many Relationships with Embedded Documents </tutorial/model-embedded-one-to-many-relationships-between-documents/>`
383373
374+ The following section shows an example of how to use the embedded document
375+ pattern.
376+
384377Embedded Document Example
385378~~~~~~~~~~~~~~~~~~~~~~~~~
386379
@@ -398,9 +391,9 @@ following example class:
398391 :language: php
399392 :dedent:
400393
401- The following sample code shows how you can create a ``SpaceShip`` model and
394+ The following sample code shows how to create a ``SpaceShip`` model and
402395embed multiple ``Cargo`` models and the MongoDB document created by running the
403- code. Click the :guilabel:`Output` button to see sample MongoDB documents
396+ code. Click the :guilabel:`View Output` button to see sample MongoDB documents
404397created by running the code:
405398
406399.. io-code-block::
@@ -478,9 +471,9 @@ The ``Passenger`` model defines a ``BelongsToMany`` relationship with
478471 :language: php
479472 :dedent:
480473
481- The following sample code shows how you can create a ``SpaceShip`` model in
474+ The following sample code shows how to create a ``SpaceShip`` model in
482475a MySQL database and related ``Passenger`` models in a MongoDB database and
483- the data created by running the code. Click the :guilabel:`Output` button to
476+ the data created by running the code. Click the :guilabel:`View Output` button to
484477see sample MongoDB documents created by running the code:
485478
486479.. io-code-block::
@@ -495,9 +488,27 @@ see sample MongoDB documents created by running the code:
495488 :language: none
496489 :visible: false
497490
491+ -- Row in the "space_ships" table
498492 +------+----------+
499493 | id | name |
500494 +------+----------+
501495 | 1234 | Nostromo |
502496 +------+----------+
503497
498+
499+ // Document in the "passengers" collection
500+ [
501+ {
502+ _id: ObjectId('65e625e74903fd63af0a5524'),
503+ name: 'Ellen Ripley',
504+ space_ship_id: 1234,
505+ // ...
506+ },
507+ {
508+ _id: ObjectId('65e625e74903fd63af0a5525'),
509+ name: 'Dwayne Hicks',
510+ space_ship_id: 1234,
511+ // ...
512+ }
513+ ]
514+
0 commit comments