@@ -14,6 +14,17 @@ Eloquent Model Relationships
1414Overview
1515--------
1616
17+ When you use a relational database, the Eloquent ORM stores models as rows
18+ in tables that correspond to the model classes. When you use MongoDB, the
19+ {+odm-short+} stores models as documents in collections that correspond to the
20+ model classes.
21+
22+ To define a relationship, add a function to the model class that calls the
23+ appropriate relationship method. This function allows you to access the related
24+ model as a **dynamic property**. A dynamic property lets you access the
25+ related model by using the same syntax as you use to access a property on the
26+ model.
27+
1728This page describes the following Laravel Eloquent and MongoDB-specific
1829relationships available in {+odm-short+} and shows examples of how to define
1930and use them:
@@ -30,24 +41,19 @@ and use them:
3041- :ref:`Cross-database relationships <laravel-relationship-cross-database>`,
3142 required when you want to create relationships between MongoDB and SQL models
3243
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.
38-
3944.. _laravel-eloquent-relationship-one-to-one:
4045
4146One to One Relationship
4247-----------------------
4348
4449A 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
50+ exactly one other type of model record.
51+
4652a document, and different model types exist in separate collections.
4753
48- When you add a one to one relationship by using the method , Eloquent lets you
49- access the model by using a dynamic property and stores the model's document
50- ID on the related model.
54+ When you add a one to one relationship, Eloquent lets you access the model by
55+ using a dynamic property and stores the model's document ID on the related
56+ model.
5157
5258In {+odm-short+}, you can define a one to one relationship by using the
5359``hasOne()`` method or ``belongsTo()`` method.
@@ -58,7 +64,7 @@ does not add any fields.
5864
5965To learn more about one to one relationships, see
6066`One to One <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#one-to-one>`__
61- in the Laravel docs .
67+ in the Laravel documentation .
6268
6369One to One Example
6470~~~~~~~~~~~~~~~~~~
@@ -70,16 +76,15 @@ relationship between a ``Planet`` and ``Orbit`` model.
7076 :language: php
7177 :dedent:
7278
73- The following example class uses a dynamic property and calls the
74- ``belongsTo()`` method to define the inverse of the relationship on ``Orbit``
75- as shown in the following example class:
79+ The following example class shows how to define the inverse relationship with
80+ ``Orbit`` by using the ``belongsTo()`` method:
7681
7782.. literalinclude:: /includes/eloquent-models/relationships/OrbitOneToOne.php
7883 :language: php
7984 :dedent:
8085
8186The following sample code shows how to instantiate a model for each class
82- and add the relationship between them. Click the :guilabel:`View Output`
87+ and add the relationship between them. Click the :guilabel:`View Output`
8388button to see the data created by running the code:
8489
8590.. io-code-block::
@@ -142,7 +147,7 @@ without adding any fields.
142147
143148To learn more about one to many relationships, see
144149`One to Many <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#one-to-many>`__
145- in the Laravel docs .
150+ in the Laravel documentation .
146151
147152One to Many Example
148153~~~~~~~~~~~~~~~~~~~
@@ -163,7 +168,7 @@ example class:
163168 :dedent:
164169
165170The following sample code shows how ton instantiate a model for each class
166- and add the relationship between them. Click the :guilabel:`View Output`
171+ and add the relationship between them. Click the :guilabel:`View Output`
167172button to see the data created by running the code:
168173
169174.. io-code-block::
@@ -213,7 +218,6 @@ the dynamic properties as defined in the example classes.
213218 :start-after: begin planet moons dynamic property example
214219 :end-before: end planet moons dynamic property example
215220
216-
217221.. _laravel-eloquent-relationship-many-to-many:
218222
219223Many to Many Relationship
@@ -239,7 +243,7 @@ document field, derived from the related model class name.
239243
240244To learn more about many to many relationships in Laravel, see
241245`Many to Many <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#many-to-many>`__
242- in the Laravel docs .
246+ in the Laravel documentation .
243247
244248The following section shows an example of how to create a many to many
245249relationship between model classes.
@@ -262,7 +266,7 @@ relationship with ``Planet`` as shown in the following example class:
262266 :dedent:
263267
264268The following sample code shows how to instantiate a model for each class
265- and add the relationship between them. Click the :guilabel:`View Output`
269+ and add the relationship between them. Click the :guilabel:`View Output`
266270button to see the data created by running the code:
267271
268272.. io-code-block::
@@ -339,20 +343,19 @@ the dynamic properties as defined in the example classes.
339343 :start-after: begin many-to-many dynamic property example
340344 :end-before: end many-to-many dynamic property example
341345
342-
343346.. _laravel-embedded-document-pattern:
344347
345348Embedded Document Pattern
346349-------------------------
347350
348351In MongoDB, the embedded document pattern adds the related model's data into
349- the parent model instead of keeping foreign key references. This pattern
350- when you must optimize for one or more of the following requirements:
352+ the parent model instead of keeping foreign key references. Use this pattern
353+ to meet one or more of the following requirements:
351354
352- - Keep associated data together in a single collection
353- - Perform atomic updates on multiple fields of the document and the associated
355+ - Keeping associated data together in a single collection
356+ - Performing atomic updates on multiple fields of the document and the associated
354357 data
355- - Reduce the number of reads required to fetch the data
358+ - Reducing the number of reads required to fetch the data
356359
357360In {+odm-short+}, you can define embedded documents by using one of the
358361following dynamic property methods:
@@ -393,7 +396,7 @@ following example class:
393396
394397The following sample code shows how to create a ``SpaceShip`` model and
395398embed multiple ``Cargo`` models and the MongoDB document created by running the
396- code. Click the :guilabel:`View Output` button to see the data created by
399+ code. Click the :guilabel:`View Output` button to see the data created by
397400running the code:
398401
399402.. io-code-block::
@@ -473,7 +476,7 @@ The ``Passenger`` model defines a ``BelongsToMany`` relationship with
473476
474477The following sample code shows how to create a ``SpaceShip`` model in
475478a MySQL database and related ``Passenger`` models in a MongoDB database and
476- the data created by running the code. Click the :guilabel:`View Output` button
479+ the data created by running the code. Click the :guilabel:`View Output` button
477480to see the data created by running the code:
478481
479482.. io-code-block::
0 commit comments