You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes you may need to load a relationship after the model has already been retrieved. For example, this may be useful if you need to dynamically decide whether to load related models. You may use `load` method on a repository to load relationships on the fly in such a case.
125
+
Sometimes you may need to load a relationship after the model has already been retrieved. For example, this may be useful if you need to decide whether to load related models dynamically. You may use the`load` method on a repository to load relationships on the fly in such a case.
Note that the `load` method will mutate the given models. It would be safer to use the method within a single `computed`method.
145
+
Note that the `load` method will mutate the given models, therefore, it is advised to use the method within a single `computed`property.
146
146
147
147
```js
148
148
import { mapRepos } from'@vuex-orm/core'
@@ -173,6 +173,34 @@ export default {
173
173
}
174
174
```
175
175
176
+
### Loading All Relationships
177
+
178
+
To load all relationships, you may use the `withAll` and `withAllRecursive` methods.
179
+
180
+
The `withAll` method will load all model level relationships. Note that any constraints will be applied to all top-level relationships.
181
+
182
+
```js
183
+
// Fetch models with all top-level relationships.
184
+
store.$repo(User).withAll().get()
185
+
186
+
// As above, with a constraint.
187
+
store.$repo(User).withAll((query) => {
188
+
// This constraint will apply to all of the relationship User has. For this
189
+
// example, all relationship will be sorted by `createdAt` field.
190
+
query.orderBy('createdAt')
191
+
}).get()
192
+
```
193
+
194
+
The `withAllRecursive` method will load all model level relationships and sub relationships recursively. By default, the maximum recursion depth is 3 when an argument is omitted.
195
+
196
+
```js
197
+
// Fetch models with relationships recursively.
198
+
store.$repo(User).withAllRecursive().get()
199
+
200
+
// As above, limiting to 2 levels deep.
201
+
store.$repo(User).withAllRecursive(2).get()
202
+
```
203
+
176
204
## Inserting Relationships
177
205
178
206
You may use `save` method to save a record with its nested relationships to the store. When saving new records into the store via `save` method, Vuex ORM automatically normalizes and stores data that contains any nested relationships in it's data structure. For example, let's say you have the `User` model that has a relationship to the `Post` model:
0 commit comments