Skip to content

Commit 63789a2

Browse files
committed
update: readme.md
1 parent 6995c5b commit 63789a2

File tree

1 file changed

+16
-55
lines changed

1 file changed

+16
-55
lines changed

readme.md

+16-55
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ class UserResource extends JsonApiResource
6464
protected function toRelationships(Request $request): iterable
6565
{
6666
return [
67-
'posts' => fn() => PostResource::collection($this->posts)->asRelationship([
67+
'posts' => PostResource::relationship(fn() => $this->posts, fn() => [
6868
'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
6969
'related' => "https://api.example.com/user/{$this->id}/posts",
7070
]),
71-
'comments' => fn() => CommentResource::collection($this->whenLoaded('comments')),
71+
'comments' => CommentResource::relationship(fn() => $this->whenLoaded('comments')),
7272
];
7373
}
7474
}
@@ -150,14 +150,14 @@ Returns resource relationships.
150150
protected function toRelationships(Request $request): array
151151
{
152152
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(),
156156
// 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() => [
158158
'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
159159
'related' => "https://api.example.com/user/{$this->id}/posts",
160-
]),
160+
])->asCollection(),
161161
];
162162
}
163163
```
@@ -173,11 +173,11 @@ Support laravel conditional relationships.
173173
protected function toRelationships(Request $request): array
174174
{
175175
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(),
181181
];
182182
}
183183
```
@@ -192,54 +192,15 @@ Returns links and meta for a relation.
192192
protected function toRelationships(Request $request): array
193193
{
194194
return [
195-
'posts' => fn () => PostResource::collection($this->posts)->asRelationship([
195+
'posts' => PostResource::relationship(fn() => $this->posts)->withLinks(fn() => [
196196
// links
197197
'self' => "https://api.example.com/user/{$this->id}/relationships/posts",
198198
'related' => "https://api.example.com/user/{$this->id}/posts",
199-
], [
199+
])->withMeta(fn() => [
200200
// meta
201201
'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(),
243204
];
244205
}
245206
```

0 commit comments

Comments
 (0)