@@ -15,7 +15,7 @@ Overview
15
15
--------
16
16
17
17
This 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
19
19
and use them:
20
20
21
21
- :ref:`One to one relationship <laravel-eloquent-relationship-one-to-one>`,
@@ -27,25 +27,23 @@ and use them:
27
27
- :ref:`Embedded document pattern <laravel-embedded-document-pattern>`, a
28
28
MongoDB-specific relationship that can represent a one to one or one to many
29
29
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
33
32
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.
39
38
40
39
.. _laravel-eloquent-relationship-one-to-one:
41
40
42
41
One to One Relationship
43
42
-----------------------
44
43
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.
49
47
50
48
When you add a one to one relationship by using the method, Eloquent lets you
51
49
access 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()``
58
56
method, Eloquent lets you access the model by using a dynamic property, but
59
57
does not add any fields.
60
58
61
- The following section shows an example of how to create a one to one
62
- relationship.
63
-
64
59
To learn more about one to one relationships, see
65
60
`One to One <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#one-to-one>`__
66
61
in the Laravel docs.
@@ -83,8 +78,8 @@ as shown in the following example class:
83
78
:language: php
84
79
:dedent:
85
80
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
88
83
see sample MongoDB documents created by running the code:
89
84
90
85
.. io-code-block::
@@ -116,7 +111,7 @@ see sample MongoDB documents created by running the code:
116
111
// ...
117
112
}
118
113
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
120
115
the dynamic properties as defined in the example classes:
121
116
122
117
.. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
@@ -131,23 +126,20 @@ One to Many Relationship
131
126
------------------------
132
127
133
128
A 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.
135
130
136
131
When you add a one to many relationship method, Eloquent lets you access the
137
132
model by using a dynamic property and stores the parent model's document ID
138
133
on each of the child model documents.
139
134
140
135
In {+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()``
142
137
method on the child class.
143
138
144
139
When you add the inverse of the relationship by using the ``belongsTo()``
145
140
method, Eloquent lets you access the parent model by using a dynamic property
146
141
without adding any fields.
147
142
148
- The following section shows an example of how to create a one to many
149
- relationship.
150
-
151
143
To learn more about one to many relationships, see
152
144
`One to Many <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#one-to-many>`__
153
145
in the Laravel docs.
@@ -163,15 +155,15 @@ relationship between a ``Planet`` parent model and ``Moon`` child model.
163
155
:dedent:
164
156
165
157
To 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
167
159
example class:
168
160
169
161
.. literalinclude:: /includes/eloquent-models/relationships/MoonOneToMany.php
170
162
:language: php
171
163
:dedent:
172
164
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
175
167
see sample MongoDB documents created by running the code:
176
168
177
169
.. io-code-block::
@@ -212,7 +204,7 @@ see sample MongoDB documents created by running the code:
212
204
}
213
205
]
214
206
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
216
208
the dynamic properties as defined in the example classes.
217
209
218
210
.. 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
236
228
237
229
When you define a many to many relationship in a relational database, Laravel
238
230
creates 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.
241
233
242
234
.. tip::
243
235
244
236
Since {+odm-short+} uses a document field instead of a pivot table, omit
245
237
the pivot table parameter from the ``belongsToMany()`` constructor or set
246
238
it to ``null``.
247
239
248
- The following section shows an example of how to create a many to many
249
- relationship between model classes.
250
-
251
240
To learn more about many to many relationships in Laravel, see
252
241
`Many to Many <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#many-to-many>`__
253
242
in the Laravel docs.
254
243
244
+ The following section shows an example of how to create a many to many
245
+ relationship between model classes.
246
+
255
247
Many to Many Example
256
248
~~~~~~~~~~~~~~~~~~~~
257
249
@@ -269,8 +261,8 @@ relationship with ``Planet`` as shown in the following example class:
269
261
:language: php
270
262
:dedent:
271
263
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
274
266
see sample MongoDB documents created by running the code:
275
267
276
268
.. io-code-block::
@@ -338,7 +330,7 @@ see sample MongoDB documents created by running the code:
338
330
}
339
331
]
340
332
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
342
334
the dynamic properties as defined in the example classes.
343
335
344
336
.. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
@@ -357,8 +349,9 @@ In MongoDB, the embedded document pattern adds the related model's data into
357
349
the parent model instead of keeping foreign key references. This pattern
358
350
when you must optimize for one or more of the following requirements:
359
351
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
362
355
- Reduce the number of reads required to fetch the data
363
356
364
357
In {+odm-short+}, you can define embedded documents by using one of the
@@ -372,15 +365,15 @@ following dynamic property methods:
372
365
These methods return Eloquent collections, which differ from query builder
373
366
objects.
374
367
375
- The following section shows an example of how to use the embedded document
376
- pattern.
377
-
378
368
To learn more about the MongoDB embedded document pattern, see the following
379
369
MongoDB server tutorials:
380
370
381
371
- :manual:`Model One-to-One Relationships with Embedded Documents </tutorial/model-embedded-one-to-one-relationships-between-documents/>`
382
372
- :manual:`Model One-to-Many Relationships with Embedded Documents </tutorial/model-embedded-one-to-many-relationships-between-documents/>`
383
373
374
+ The following section shows an example of how to use the embedded document
375
+ pattern.
376
+
384
377
Embedded Document Example
385
378
~~~~~~~~~~~~~~~~~~~~~~~~~
386
379
@@ -398,9 +391,9 @@ following example class:
398
391
:language: php
399
392
:dedent:
400
393
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
402
395
embed 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
404
397
created by running the code:
405
398
406
399
.. io-code-block::
@@ -478,9 +471,9 @@ The ``Passenger`` model defines a ``BelongsToMany`` relationship with
478
471
:language: php
479
472
:dedent:
480
473
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
482
475
a 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
484
477
see sample MongoDB documents created by running the code:
485
478
486
479
.. io-code-block::
@@ -495,9 +488,27 @@ see sample MongoDB documents created by running the code:
495
488
:language: none
496
489
:visible: false
497
490
491
+ -- Row in the "space_ships" table
498
492
+------+----------+
499
493
| id | name |
500
494
+------+----------+
501
495
| 1234 | Nostromo |
502
496
+------+----------+
503
497
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