Skip to content

Commit 6875660

Browse files
author
Chris Cho
committed
add missing files
1 parent 8228f51 commit 6875660

File tree

4 files changed

+124
-1
lines changed

4 files changed

+124
-1
lines changed

docs/eloquent-models.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ syntax to work with MongoDB as a database.
1919
This section contains guidance on how to use Eloquent models in
2020
{+odm-short+} to work with MongoDB in the following ways:
2121

22-
- :ref:`laravel-eloquent-model-relationships`` shows how to add relationships
22+
- :ref:`laravel-eloquent-model-relationships` shows how to add relationships
2323
between models
2424

2525
.. toctree::

docs/eloquent-models/relationships.txt

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,95 @@ in MongoDB:
219219
Many 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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
use MongoDB\Laravel\Relations\BelongsTo;
7+
8+
class Moon extends Model
9+
{
10+
protected $connection = 'mongodb';
11+
12+
public function planet(): BelongsTo
13+
{
14+
return $this->belongsTo(Planet::class);
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use MongoDB\Laravel\Eloquent\Model;
6+
use MongoDB\Laravel\Relations\HasMany;
7+
8+
class Planet extends Model
9+
{
10+
protected $connection = 'mongodb';
11+
12+
public function moons(): HasMany
13+
{
14+
return $this->hasMany(Moon::class);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)