From 756737e2f918ca9a559ac0b8cf3f5f400607243c Mon Sep 17 00:00:00 2001 From: Nick Sagona Date: Fri, 9 Oct 2020 09:14:11 -0500 Subject: [PATCH] Update docs --- reference/pop_db.rst | 27 +++++++++++++++++++++++---- user_guide/databases.rst | 27 +++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/reference/pop_db.rst b/reference/pop_db.rst index f81722e..c4b4077 100644 --- a/reference/pop_db.rst +++ b/reference/pop_db.rst @@ -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, diff --git a/user_guide/databases.rst b/user_guide/databases.rst index 09cc003..45467e7 100644 --- a/user_guide/databases.rst +++ b/user_guide/databases.rst @@ -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,