File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,35 @@ export default class Post extends Model {
156156Now we can easily access an instance of the ** User** model containing the eager loaded data
157157using the specified key: ` post.user `
158158
159+ The ` relations ` method also support nested keys, by dot notation:
160+
161+ ``` js{}[~/models/Post.js]
162+ import Model from './Model'
163+ import User from './User'
164+ import Comment from './Comment'
165+
166+ export default class Post extends Model {
167+ // Set the resource route of the model
168+ resource() {
169+ return 'posts'
170+ }
171+
172+ // Define the primary key of the model
173+ primaryKey() {
174+ return 'slug'
175+ }
176+
177+ // Apply model instances to eager loaded relationships
178+ relations() {
179+ return {
180+ 'relationships.user': User,
181+ 'relationships.comments': Comment
182+ }
183+ }
184+ ```
185+
186+ Then we can access using the specified key: ` post.relationships.user `
187+
159188### Lazy Loading Relationships
160189
161190See the [ API reference] ( /api/model-options#hasmany )
You can’t perform that action at this time.
0 commit comments