Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the use of load(on:) in relations.md #956

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/fluent/relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ See [query](query.md) for more information.

## Eager Loading

Fluent's query builder allows you to preload a model's relations when it is fetched from the database. This is called eager loading and allows you to access relations synchronously without needing to call [`load`](#lazy-eager-loading) or [`get`](#get) first.
Fluent's query builder allows you to preload a model's relations when it is fetched from the database. This is called eager loading and allows you to access relations synchronously without needing to call [`get`](#get) first.

To eager load a relation, pass a key path to the relation to the `with` method on query builder.

Expand Down Expand Up @@ -331,16 +331,23 @@ The `with` method accepts an optional closure as a second parameter. This closur

## Lazy Eager Loading

In case that you have already retrieved the parent model and you want to load one of it's relations, you can use the `load(on:)` method for that purpose. This will fetch the related model from the database and allows it to be accessed as a local property.
In case that you have already retrieved the parent model and you want to load one of it's relations, you can use the `get(reload:on:)` method for that purpose. This will fetch the related model from the database (or cache, if available) and allows it to be accessed as a local property.

```swift
planet.$star.load(on: database).map {
planet.$star.get(on: database).map {
print(planet.star.name)
}

// Or

try await planet.$star.load(on: database)
try await planet.$star.get(on: database)
print(planet.star.name)
```

In case you want to ensure that the data you receive is not pulled from cache, use the `reload:` parameter.

```swift
try await planet.$star.get(reload: true, on: database)
print(planet.star.name)
```

Expand Down