Skip to content

Commit bbbd8fb

Browse files
committed
docs update - deep relations
1 parent ece5dca commit bbbd8fb

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

docs/basic_usage.md

+22
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,32 @@ model(UserModel::class)->with('profile')->findAll();
3434

3535
This will perform two queries. One for all users, and one to fetch all the profiles for these users.
3636

37+
The data for the relation will be available under the relation name, in this case `$user->profile`. Data format will respect the `$returnType` set in the `ProfileModel` class.
38+
3739
!!! note
3840

3941
You can still use your model as usual. If you omit the `with()` part, your model will work like a normal model.
4042

43+
### Deep relations
44+
45+
We can also call deep relations. This will query all posts for user and then all the comments for posts:
46+
47+
```php
48+
model(UserModel::class)->with('posts')->with('posts.comments')->find(1);
49+
```
50+
51+
The `'posts.comments'` relation mean that we will be looking for `comments` relation in the `PostModel` class.
52+
53+
We can go even further and get only comments that were created by user we're looking for:
54+
55+
```php
56+
model(UserModel::class)->with('posts')->with('posts.comments', static function (Model $model) {
57+
$model->where('comments.user_id', 1);
58+
})->find(1);
59+
```
60+
61+
This will again query all posts for user but then will get only comments for posts that were created by user with ID `1`.
62+
4163
### Writing with relation
4264

4365
For simple relations like `hasOne` or `hasMany`, you can also save the entire object that contains relations inside.

0 commit comments

Comments
 (0)