@@ -64,11 +64,11 @@ class UserResource extends JsonApiResource
64
64
protected function toRelationships(Request $request): iterable
65
65
{
66
66
return [
67
- 'posts' => fn() => PostResource::collection( $this->posts)->asRelationship( [
67
+ 'posts' => PostResource::relationship( fn() => $this->posts, fn() => [
68
68
'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
69
69
'related' => "https://api.example.com/user/{$this->id}/posts",
70
70
]),
71
- 'comments' => fn() => CommentResource::collection( $this->whenLoaded('comments')),
71
+ 'comments' => CommentResource::relationship( fn() => $this->whenLoaded('comments')),
72
72
];
73
73
}
74
74
}
@@ -150,14 +150,14 @@ Returns resource relationships.
150
150
protected function toRelationships(Request $request): array
151
151
{
152
152
return [
153
- 'avatar' => AvatarResource::make ($this->avatar),
154
- // with lazy evaluation
155
- 'comments' => fn() => CommentResource::collection( $this->whenLoaded('comments')),
153
+ 'avatar' => AvatarResource::relationship ($this->avatar),
154
+ // as collection, with condition
155
+ 'comments' => CommentResource::relationship( fn() => $this->whenLoaded('comments'))->asCollection( ),
156
156
// with relationship (allow to include links and meta on relation)
157
- 'posts' => fn() => PostResource::collection( $this->posts)->asRelationship( [
157
+ 'posts' => PostResource::relationship( fn() => $this->posts)->withLinks(fn() => [
158
158
'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
159
159
'related' => "https://api.example.com/user/{$this->id}/posts",
160
- ]),
160
+ ])->asCollection() ,
161
161
];
162
162
}
163
163
```
@@ -173,11 +173,11 @@ Support laravel conditional relationships.
173
173
protected function toRelationships(Request $request): array
174
174
{
175
175
return [
176
- 'avatar' => AvatarResource::make ($this->avatar),
177
- // with lazy evaluation
178
- 'posts ' => fn () => PostResource::collection( $this->posts ),
179
- // with laravel conditional relationships
180
- 'comments ' => fn() => CommentResource::collection( $this->whenLoaded('comments') ),
176
+ 'avatar' => AvatarResource::relationship ($this->avatar),
177
+ // as collection, with condition
178
+ 'comments ' => CommentResource::relationship(fn () => $this->whenLoaded('comments'))->asCollection( ),
179
+ // with relationship (allow to include links and meta on relation)
180
+ 'posts ' => PostResource::relationship( fn() => $this->posts)->asCollection( ),
181
181
];
182
182
}
183
183
```
@@ -192,54 +192,15 @@ Returns links and meta for a relation.
192
192
protected function toRelationships(Request $request): array
193
193
{
194
194
return [
195
- 'posts' => fn () => PostResource::collection( $this->posts)->asRelationship( [
195
+ 'posts' => PostResource::relationship(fn () => $this->posts)->withLinks(fn() => [
196
196
// links
197
197
'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
198
198
'related' => "https://api.example.com/user/{$this->id}/posts",
199
- ], [
199
+ ])->withMeta(fn() => [
200
200
// meta
201
201
'creator' => $this->name,
202
- ]),
203
- ];
204
- }
205
- ```
206
-
207
- Define with a Closure :
208
-
209
- ``` php
210
- protected function toRelationships(Request $request): array
211
- {
212
- return [
213
- 'posts' => fn () => PostResource::collection($this->posts)->asRelationship(
214
- // links
215
- fn($collection) => [
216
- 'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
217
- 'related' => "https://api.example.com/user/{$this->id}/posts",
218
- ],
219
- // meta
220
- fn($collection) => [
221
- 'creator' => $this->name,
222
- ]
223
- ),
224
- ];
225
- }
226
- ```
227
-
228
- Define with methods :
229
-
230
- ``` php
231
- protected function toRelationships(Request $request): array
232
- {
233
- return [
234
- 'posts' => fn () => PostResource::collection($this->posts)
235
- ->asRelationship()
236
- ->withLinks([
237
- 'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
238
- 'related' => "https://api.example.com/user/{$this->id}/posts",
239
- ])
240
- ->withMeta([
241
- 'creator' => $this->name,
242
- ]),
202
+ ])
203
+ ->asCollection(),
243
204
];
244
205
}
245
206
```
0 commit comments