Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksagona committed Oct 9, 2020
1 parent 5c6a244 commit 756737e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
27 changes: 23 additions & 4 deletions reference/pop_db.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1032,17 +1032,36 @@ set up, you'll actually access it using the static ``with()`` method, like this:
echo $order->id;
}
The static ``with()`` method also supports nested child relationships:
The static ``with()`` method also supports multiple relationships as well:

.. code-block:: php
$user = Users::with('users.posts.comments')->getById(1001);
$user = Users::with(['orders', 'posts'])->getById(1001);
The static ``with()`` method also supports multiple relationships as well:
The static ``with()`` method also supports nested child relationships. Assuming the following relationship methods
exist in the following classes:

* ``Users->posts()`` - returns `$this->hasMany()`
* ``Posts->comments()`` - returns `$this->hasMany()`
* ``Comments->responses()`` - returns `$this->hasMany()`

this following example will retrieve ``parent -> child -> grandchild``:

.. code-block:: php
$user = Users::with(['orders', 'posts'])->getById(1001);
$user = Users::with('posts.comments.responses')->getById(1001);
And, it works in reverse as well. Assuming the following relationship methods exist in the following classes:

* ``Responses->comment()`` - returns `$this->belongsTo()`
* ``Comments->post()`` - returns `$this->belongsTo()`
* ``Posts->user()`` - returns `$this->belongsTo()`

this following example will retrieve grandchild -> child -> parent:

.. code-block:: php
$response = Responses::with('comment.post.user')->getById(1001);
A note about the access in the example given above. Even though a method was defined to access the different
relationships, you can use a magic property to access them as well, and it will route to that method. Also,
Expand Down
27 changes: 23 additions & 4 deletions user_guide/databases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1032,17 +1032,36 @@ set up, you'll actually access it using the static ``with()`` method, like this:
echo $order->id;
}
The static ``with()`` method also supports nested child relationships:
The static ``with()`` method also supports multiple relationships as well:

.. code-block:: php
$user = Users::with('users.posts.comments')->getById(1001);
$user = Users::with(['orders', 'posts'])->getById(1001);
The static ``with()`` method also supports multiple relationships as well:
The static ``with()`` method also supports nested child relationships. Assuming the following relationship methods
exist in the following classes:

* ``Users->posts()`` - returns `$this->hasMany()`
* ``Posts->comments()`` - returns `$this->hasMany()`
* ``Comments->responses()`` - returns `$this->hasMany()`

this following example will retrieve ``parent -> child -> grandchild``:

.. code-block:: php
$user = Users::with(['orders', 'posts'])->getById(1001);
$user = Users::with('posts.comments.responses')->getById(1001);
And, it works in reverse as well. Assuming the following relationship methods exist in the following classes:

* ``Responses->comment()`` - returns `$this->belongsTo()`
* ``Comments->post()`` - returns `$this->belongsTo()`
* ``Posts->user()`` - returns `$this->belongsTo()`

this following example will retrieve grandchild -> child -> parent:

.. code-block:: php
$response = Responses::with('comment.post.user')->getById(1001);
A note about the access in the example given above. Even though a method was defined to access the different
relationships, you can use a magic property to access them as well, and it will route to that method. Also,
Expand Down

0 comments on commit 756737e

Please sign in to comment.