Skip to content

Commit

Permalink
docs update - deep relations
Browse files Browse the repository at this point in the history
  • Loading branch information
michalsn committed Dec 13, 2024
1 parent ece5dca commit bbbd8fb
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions docs/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,32 @@ model(UserModel::class)->with('profile')->findAll();

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

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.

!!! note

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

### Deep relations

We can also call deep relations. This will query all posts for user and then all the comments for posts:

```php
model(UserModel::class)->with('posts')->with('posts.comments')->find(1);
```

The `'posts.comments'` relation mean that we will be looking for `comments` relation in the `PostModel` class.

We can go even further and get only comments that were created by user we're looking for:

```php
model(UserModel::class)->with('posts')->with('posts.comments', static function (Model $model) {
$model->where('comments.user_id', 1);
})->find(1);
```

This will again query all posts for user but then will get only comments for posts that were created by user with ID `1`.

### Writing with relation

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

0 comments on commit bbbd8fb

Please sign in to comment.