@@ -11,28 +11,102 @@ Eloquent Model Relationships
1111.. meta::
1212 :keywords: php framework, odm, code example
1313
14- Relationships
15- -------------
14+ Overview
15+ --------
16+
17+ This section describes the following Laravel Eloquent and MongoDB-specific
18+ relationships available in {+odm-short+} and shows examples on how to define
19+ and use them:
20+
21+ - :ref:`One to one relationship <laravel-eloquent-relationship-one-to-one>`,
22+ created by using the ``hasOne()`` method and its inverse, ``belongsTo()``
23+ - :ref:`One to many relationship <laravel-eloquent-relationship-one-to-many>`,
24+ created by using the ``hasMany()`` and its inverse, ``belongsTo()``
25+ - :ref:`Many to many relationship <laravel-eloquent-relationship-many-to-many>`,
26+ created by using the ``belongsToMany()`` method
27+ - :ref:`Embedded document pattern <laravel-embedded-document-pattern>`,
28+ specific to MongoDB, which can represent a one
29+ to one or one to many relationship by using the ``embedsOne()`` or
30+ ``embedsMany()`` method
31+
32+
33+ TODO:
34+ Each example will show, when applicable, how to define the relationships,
35+ access the related models, save/create, destroy/delete, and associate/disassociate.
1636
17- This section describes each of the Laravel Eloquent and MongoDB-specific
18- relationships that you can use and provides examples for hasOne, hasMany,
19- belongsTo, belongsToMany, embedsOne, and embedsMany.
37+ .. _laravel-eloquent-relationship-one-to-one:
2038
39+ One to One Relationship
40+ -----------------------
2141
22- {+odm-short+} supports the following Eloquent relationships:
42+ In data modeling, a one to one relationship between models means that one model
43+ record is related to exactly one other type of model record. In {+odm-short+},
44+ this is represented by storing the value of the ID of the related model.
2345
24- - hasOne
25- - hasMany
26- - belongsTo
27- - belongsToMany
46+ TODO
2847
48+ To establish a one to one relationship, define a method that returns a
49+ ``hasOne`` function and calls the ``hasOne()`` method. Pass it the class of
50+ the related model.
2951
30- It also supports the following
52+ To define the inverse of the relationship on the related model and call the
53+ ``belongsTo()`` method on it.
3154
55+ https://laravel.com/docs/10.x/eloquent-relationships#one-to-one
3256
3357
34- Each example will show, when applicable, how to define the relationships,
35- access the related models, save/create, destroy/delete, and associate/disassociate.
58+ One to One Example
59+ ~~~~~~~~~~~~~~~~~~
60+
61+ The following classes shows a one to one relationship between a ``Planet``
62+ and ``Orbit`` model.
63+
64+ .. literalinclude:: /includes/eloquent-models/relationships/PlanetOneToOne.php
65+ :language: php
66+ :dedent:
67+
68+ .. literalinclude:: /includes/eloquent-models/relationships/OrbitOneToOne.php
69+ :language: php
70+ :dedent:
71+
72+ The following sample code shows how you can create a model and ould in
73+ To create a Planet and corresponding Orbit,
74+
75+
76+ .. literalinclude:: /includes/eloquent-models/relationships/RelationshipController
77+ :language: php
78+ :dedent:
79+ :starts-after: begin one-to-one save
80+ :ends-before: end one-to-one save
81+
82+
83+ MongoDB documents:
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+ .. _laravel-eloquent-relationship-one-to-many:
95+
96+ One to Many Relationship
97+ ------------------------
98+
99+ .. _laravel-eloquent-relationship-many-to-many:
100+
101+ Many to Many Relationship
102+ -------------------------
103+
104+
105+ .. _laravel-embedded-document-pattern:
106+
107+ Embedded Document Pattern
108+ -------------------------
109+
36110
37111
38112
0 commit comments