Skip to content

Commit 49bcdef

Browse files
author
Chris Cho
committed
one to one
1 parent 28737cd commit 49bcdef

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

docs/eloquent-models/relationships.txt

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Eloquent Model Relationships
99
:values: tutorial
1010

1111
.. meta::
12-
:keywords: php framework, odm, code example
12+
:keywords: php framework, odm, code example, entity relationship, eloquent
1313

1414
Overview
1515
--------
@@ -29,67 +29,96 @@ and use them:
2929
to one or one to many relationship by using the ``embedsOne()`` or
3030
``embedsMany()`` method
3131

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.
3635

3736
.. _laravel-eloquent-relationship-one-to-one:
3837

3938
One to One Relationship
4039
-----------------------
4140

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.
4545

46-
TODO
46+
In {+odm-short+}, you can define a one to one relationship by using the
47+
``hasOne()`` method or ``belongsTo()`` method.
4748

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.
5152

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.
5456

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.
5659

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.
5763

5864
One to One Example
5965
~~~~~~~~~~~~~~~~~~
6066

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.
6369

6470
.. literalinclude:: /includes/eloquent-models/relationships/PlanetOneToOne.php
6571
:language: php
6672
:dedent:
6773

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+
6878
.. literalinclude:: /includes/eloquent-models/relationships/OrbitOneToOne.php
6979
:language: php
7080
:dedent:
7181

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:
7484

75-
76-
.. literalinclude:: /includes/eloquent-models/relationships/RelationshipController
85+
.. literalinclude:: /includes/eloquent-models/relationships/RelationshipController.php
7786
:language: php
7887
:dedent:
7988
:starts-after: begin one-to-one save
8089
:ends-before: end one-to-one save
8190

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:
8293

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+
}
93122

94123
.. _laravel-eloquent-relationship-one-to-many:
95124

docs/includes/eloquent-models/relationships/RelationshipController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ public function store(Request $request)
5555
public function show()
5656
{
5757

58-
// begin planet-to-orbit
58+
// begin dynamic property example
5959
$planet = Planet::first();
6060
$related_orbit = $planet->orbit;
61-
// end planet-to-orbit
6261

63-
// begin orbit-to-planet
6462
$orbit = Orbit::first();
6563
$related_planet = $orbit->planet;
66-
// end orbit-to-planet
64+
// end dynamic property example
6765

6866
return view('browse_planets', [
6967
'planets' => Planet::take(10)

0 commit comments

Comments
 (0)