@@ -14,6 +14,17 @@ Eloquent Model Relationships
14
14
Overview
15
15
--------
16
16
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
+
17
28
This page describes the following Laravel Eloquent and MongoDB-specific
18
29
relationships available in {+odm-short+} and shows examples of how to define
19
30
and use them:
@@ -30,24 +41,19 @@ and use them:
30
41
- :ref:`Cross-database relationships <laravel-relationship-cross-database>`,
31
42
required when you want to create relationships between MongoDB and SQL models
32
43
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
-
39
44
.. _laravel-eloquent-relationship-one-to-one:
40
45
41
46
One to One Relationship
42
47
-----------------------
43
48
44
49
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
50
+ exactly one other type of model record.
51
+
46
52
a document, and different model types exist in separate collections.
47
53
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.
51
57
52
58
In {+odm-short+}, you can define a one to one relationship by using the
53
59
``hasOne()`` method or ``belongsTo()`` method.
@@ -58,7 +64,7 @@ does not add any fields.
58
64
59
65
To learn more about one to one relationships, see
60
66
`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 .
62
68
63
69
One to One Example
64
70
~~~~~~~~~~~~~~~~~~
@@ -70,16 +76,15 @@ relationship between a ``Planet`` and ``Orbit`` model.
70
76
:language: php
71
77
:dedent:
72
78
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:
76
81
77
82
.. literalinclude:: /includes/eloquent-models/relationships/OrbitOneToOne.php
78
83
:language: php
79
84
:dedent:
80
85
81
86
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`
87
+ and add the relationship between them. Click the :guilabel:`View Output`
83
88
button to see the data created by running the code:
84
89
85
90
.. io-code-block::
@@ -142,7 +147,7 @@ without adding any fields.
142
147
143
148
To learn more about one to many relationships, see
144
149
`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 .
146
151
147
152
One to Many Example
148
153
~~~~~~~~~~~~~~~~~~~
@@ -163,7 +168,7 @@ example class:
163
168
:dedent:
164
169
165
170
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`
171
+ and add the relationship between them. Click the :guilabel:`View Output`
167
172
button to see the data created by running the code:
168
173
169
174
.. io-code-block::
@@ -213,7 +218,6 @@ the dynamic properties as defined in the example classes.
213
218
:start-after: begin planet moons dynamic property example
214
219
:end-before: end planet moons dynamic property example
215
220
216
-
217
221
.. _laravel-eloquent-relationship-many-to-many:
218
222
219
223
Many to Many Relationship
@@ -239,7 +243,7 @@ document field, derived from the related model class name.
239
243
240
244
To learn more about many to many relationships in Laravel, see
241
245
`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 .
243
247
244
248
The following section shows an example of how to create a many to many
245
249
relationship between model classes.
@@ -262,7 +266,7 @@ relationship with ``Planet`` as shown in the following example class:
262
266
:dedent:
263
267
264
268
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`
269
+ and add the relationship between them. Click the :guilabel:`View Output`
266
270
button to see the data created by running the code:
267
271
268
272
.. io-code-block::
@@ -339,20 +343,19 @@ the dynamic properties as defined in the example classes.
339
343
:start-after: begin many-to-many dynamic property example
340
344
:end-before: end many-to-many dynamic property example
341
345
342
-
343
346
.. _laravel-embedded-document-pattern:
344
347
345
348
Embedded Document Pattern
346
349
-------------------------
347
350
348
351
In 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:
351
354
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
354
357
data
355
- - Reduce the number of reads required to fetch the data
358
+ - Reducing the number of reads required to fetch the data
356
359
357
360
In {+odm-short+}, you can define embedded documents by using one of the
358
361
following dynamic property methods:
@@ -393,7 +396,7 @@ following example class:
393
396
394
397
The following sample code shows how to create a ``SpaceShip`` model and
395
398
embed 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
397
400
running the code:
398
401
399
402
.. io-code-block::
@@ -473,7 +476,7 @@ The ``Passenger`` model defines a ``BelongsToMany`` relationship with
473
476
474
477
The following sample code shows how to create a ``SpaceShip`` model in
475
478
a 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
477
480
to see the data created by running the code:
478
481
479
482
.. io-code-block::
0 commit comments