@@ -9,7 +9,7 @@ Eloquent Model Relationships
9
9
:values: tutorial
10
10
11
11
.. meta::
12
- :keywords: php framework, odm, code example
12
+ :keywords: php framework, odm, code example, entity relationship, eloquent
13
13
14
14
Overview
15
15
--------
@@ -29,67 +29,96 @@ and use them:
29
29
to one or one to many relationship by using the ``embedsOne()`` or
30
30
``embedsMany()`` method
31
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.
32
+ To establish a relationship, add an Eloquent **dynamic property** to access the
33
+ related model. The dynamic property lets you access the relational method by
34
+ using the same syntax as you use to access a property on the model.
36
35
37
36
.. _laravel-eloquent-relationship-one-to-one:
38
37
39
38
One to One Relationship
40
39
-----------------------
41
40
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.
41
+ A one to one relationship between models consists of a model record that
42
+ is related to exactly one other type of model record. In MongoDB, a record
43
+ is represented as a document and different model types exist in separate
44
+ collections.
45
45
46
- TODO
46
+ In {+odm-short+}, you can define a one to one relationship by using the
47
+ ``hasOne()`` method or ``belongsTo()`` method.
47
48
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.
49
+ When you establish a one to one relationship by using the ``hasOne()`` method,
50
+ Eloquent lets you access the model by using a dynamic property and stores the
51
+ model's ID on the related model.
51
52
52
- To define the inverse of the relationship on the related model and call the
53
- ``belongsTo()`` method on it.
53
+ When you establish the inverse of the relationship by using the ``belongsTo()``
54
+ method, Eloquent lets you access the model by using a dynamic property, but
55
+ does not add any fields.
54
56
55
- https://laravel.com/docs/10.x/eloquent-relationships#one-to-one
57
+ The following section shows an example of how to create one to one
58
+ relationships.
56
59
60
+ To learn more about one to one relationships, see
61
+ `One to One <https://laravel.com/docs/10.x/eloquent-relationships#one-to-one>`__
62
+ in the Laravel docs.
57
63
58
64
One to One Example
59
65
~~~~~~~~~~~~~~~~~~
60
66
61
- The following classes shows a one to one relationship between a ``Planet``
62
- and ``Orbit`` model.
67
+ The following example class shows how to define a ``HasOne`` one to one
68
+ relationship between a ``Planet`` and ``Orbit`` model.
63
69
64
70
.. literalinclude:: /includes/eloquent-models/relationships/PlanetOneToOne.php
65
71
:language: php
66
72
:dedent:
67
73
74
+ To define the inverse of the relationship on ``Orbit``, add the dynamic
75
+ property and call the ``belongsTo()`` method on it as shown in the following
76
+ example class:
77
+
68
78
.. literalinclude:: /includes/eloquent-models/relationships/OrbitOneToOne.php
69
79
:language: php
70
80
:dedent:
71
81
72
- The following sample code shows how you can create a model and ould in
73
- To create a Planet and corresponding Orbit,
82
+ The following sample code shows how you can create a model for each class
83
+ and add the relationship between them:
74
84
75
-
76
- .. literalinclude:: /includes/eloquent-models/relationships/RelationshipController
85
+ .. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
77
86
:language: php
78
87
:dedent:
79
88
:starts-after: begin one-to-one save
80
89
:ends-before: end one-to-one save
81
90
91
+ The following sample code shows how you can access the related models by using
92
+ the dynamic properties as defined in the example classes:
82
93
83
- MongoDB documents:
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
94
+ .. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
95
+ :language: php
96
+ :dedent:
97
+ :starts-after: begin dynamic property example
98
+ :ends-before: end dynamic property example
99
+
100
+ The models created in the prior examples resemble the following documents
101
+ in MongoDB:
102
+
103
+ .. code-block:: javascript
104
+ :copyable: false
105
+
106
+ // Document in the "planets" collection
107
+ {
108
+ _id: ObjectId('65de67fb2e59d63e6d07f8b8'),
109
+ name: 'Earth',
110
+ diameter_km: 12742,
111
+ // ...
112
+ }
113
+
114
+ // Document in the "orbits" collection
115
+ {
116
+ _id: ObjectId('65de67fb2e59d63e6d07f8b9'),
117
+ period: 365.26,
118
+ direction: 'counterclockwise',
119
+ planet_id: '65de67fb2e59d63e6d07f8b8',
120
+ // ...
121
+ }
93
122
94
123
.. _laravel-eloquent-relationship-one-to-many:
95
124
0 commit comments