Skip to content

Commit 28737cd

Browse files
author
Chris Cho
committed
WIP
1 parent ef774e2 commit 28737cd

File tree

4 files changed

+218
-13
lines changed

4 files changed

+218
-13
lines changed

docs/eloquent-models/relationships.txt

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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 Orbit 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\HasOne;
7+
8+
class Planet extends Model
9+
{
10+
protected $connection = 'mongodb';
11+
12+
public function orbit(): HasOne
13+
{
14+
return $this->hasOne(Orbit::class);
15+
}
16+
17+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\Planet;
6+
use App\Models\Orbit;
7+
use Illuminate\Http\Request;
8+
9+
class PlanetController extends Controller
10+
{
11+
/**
12+
* Display a listing of the resource.
13+
*/
14+
public function index()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Show the form for creating a new resource.
21+
*/
22+
public function create()
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Store a newly created resource in storage.
29+
*/
30+
public function store(Request $request)
31+
{
32+
33+
// begin one-to-one save
34+
$planet = new Planet();
35+
$planet->name = 'Earth';
36+
$planet->diameter_km = 12742;
37+
$planet->save();
38+
39+
$orbit = new Orbit();
40+
$orbit->period = 365.26;
41+
$orbit->direction = 'counterclockwise';
42+
43+
$planet->orbit()->save($orbit);
44+
// end one-to-one save
45+
46+
47+
48+
return;
49+
}
50+
51+
/**
52+
* Display the specified resource.
53+
*/
54+
55+
public function show()
56+
{
57+
58+
// begin planet-to-orbit
59+
$planet = Planet::first();
60+
$related_orbit = $planet->orbit;
61+
// end planet-to-orbit
62+
63+
// begin orbit-to-planet
64+
$orbit = Orbit::first();
65+
$related_planet = $orbit->planet;
66+
// end orbit-to-planet
67+
68+
return view('browse_planets', [
69+
'planets' => Planet::take(10)
70+
->get()
71+
]);
72+
}
73+
74+
/**
75+
* Show the form for editing the specified resource.
76+
*/
77+
public function edit(Planet $planet)
78+
{
79+
//
80+
}
81+
82+
/**
83+
* Update the specified resource in storage.
84+
*/
85+
public function update(Request $request, Planet $planet)
86+
{
87+
//
88+
}
89+
90+
/**
91+
* Remove the specified resource from storage.
92+
*/
93+
public function destroy(Planet $planet)
94+
{
95+
//
96+
}
97+
}

0 commit comments

Comments
 (0)