@@ -219,6 +219,95 @@ in MongoDB:
219219Many to Many Relationship
220220-------------------------
221221
222+ A many to many relationship consists of a relationship between two different
223+ model types in which each model record can be related to multiple records
224+ of the other type.
225+
226+ In {+odm-short+}, you can define a many to many relationship by using the
227+ ``belongsToMany()`` method.
228+
229+ When you add a many to many relationship by using the ``belongsTo()`` method,
230+ Eloquent lets you access the model by using a dynamic property and stores the
231+ parent model's document ID on each of the child model documents.
232+
233+ When you add the inverse of the relationship by using the ``belongsTo()``
234+ method, Eloquent lets you access the parent model by using a dynamic property,
235+ but does not add any fields.
236+
237+ The following section shows an example of how to create one to many
238+ relationships.
239+
240+ To learn more about one to many relationships, see
241+ `One to Many <https://laravel.com/docs/{+laravel-docs-version+}/eloquent-relationships#one-to-many>`__
242+ in the Laravel docs.
243+
244+ One to Many Example
245+ ~~~~~~~~~~~~~~~~~~~
246+
247+ The following example class shows how to define a ``HasMany`` one to many
248+ relationship between a ``Planet`` parent model and ``Moon`` child model.
249+
250+ .. literalinclude:: /includes/eloquent-models/relationships/PlanetOneToMany.php
251+ :language: php
252+ :dedent:
253+
254+ To define the inverse of the relationship on ``Moon``, add the dynamic
255+ property and call the ``belongsTo()`` method on it as shown in the following
256+ example class:
257+
258+ .. literalinclude:: /includes/eloquent-models/relationships/MoonOneToMany.php
259+ :language: php
260+ :dedent:
261+
262+ The following sample code shows how you can create a model for each class
263+ and add the relationship between them:
264+
265+ .. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
266+ :language: php
267+ :dedent:
268+ :start-after: begin one-to-many save
269+ :end-before: end one-to-many save
270+
271+ The following sample code shows how you can access the related models by using
272+ the dynamic properties as defined in the example classes:
273+
274+ .. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
275+ :language: php
276+ :dedent:
277+ :start-after: begin planet moons dynamic property example
278+ :end-before: end planet moons dynamic property example
279+
280+ The models created in the prior examples resemble the following documents
281+ in MongoDB:
282+
283+ .. code-block:: javascript
284+ :copyable: false
285+
286+ // Parent document in the "planets" collection
287+ {
288+ _id: ObjectId('65dfb0050e323bbef800f7b2'),
289+ name: 'Jupiter',
290+ diameter_km: 142984,
291+ // ...
292+ }
293+
294+ // Child documents in the "moons" collection
295+ [
296+ {
297+ _id: ObjectId('65dfb0050e323bbef800f7b3'),
298+ name: 'Ganymede',
299+ orbital_period: 7.15,
300+ planet_id: '65dfb0050e323bbef800f7b2',
301+ // ...
302+ },
303+ {
304+ _id: ObjectId('65dfb0050e323bbef800f7b4'),
305+ name: 'Europa',
306+ orbital_period: 3.55,
307+ planet_id: '65dfb0050e323bbef800f7b2',
308+ // ...
309+ }
310+ ]
222311
223312.. _laravel-embedded-document-pattern:
224313
0 commit comments