Skip to content

Commit 4f421f7

Browse files
committed
fix: describer:relation->meta
1 parent ab57c9a commit 4f421f7

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/Descriptors/Relations/Relation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function links(Closure $links): static
5353

5454
public function meta(Closure $meta): static
5555
{
56-
$this->links = $meta;
56+
$this->meta = $meta;
5757
return $this;
5858
}
5959

tests/Feature/User/ResourceTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ private function getJsonResult(User $user, ?array $attributes = null, ?array $re
160160
'links' => [
161161
'self' => "https://api.example.com/user/{$user->id}/relationships/posts",
162162
'related' => "https://api.example.com/user/{$user->id}/posts",
163+
],
164+
'meta' => [
165+
'total' => $user->posts->count()
163166
]
164167
]),
165168
'comments' => array_filter([
@@ -173,6 +176,9 @@ private function getJsonResult(User $user, ?array $attributes = null, ?array $re
173176
'links' => [
174177
'self' => "https://api.example.com/user/{$user->id}/relationships/comments",
175178
'related' => "https://api.example.com/user/{$user->id}/comments",
179+
],
180+
'meta' => [
181+
'total' => $user->comments->count()
176182
]
177183
]),
178184
],

tests/app/Http/Resources/PostResource.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ protected function toRelationships(Request $request): iterable
4141
'comments' => $this->many(CommentResource::class)->links(fn() => [
4242
'self' => "https://api.example.com/posts/{$this->resource->id}/relationships/comments",
4343
'related' => "https://api.example.com/posts/{$this->resource->id}/comments",
44+
])->meta(fn() => [
45+
'total' => $this->resource->comments()->count(),
4446
]),
4547
];
4648
}

tests/app/Http/Resources/UserResource.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ protected function toResourceMeta(Request $request): ?iterable
3939
protected function toRelationships(Request $request): iterable
4040
{
4141
return [
42-
'posts' => $this->many(PostResource::class)->links(fn() => [
42+
'posts' => PostResource::relationship(fn() => $this->resource->posts, fn() => [
4343
'self' => "https://api.example.com/user/{$this->resource->id}/relationships/posts",
4444
'related' => "https://api.example.com/user/{$this->resource->id}/posts",
45-
]),
45+
], fn() => [
46+
'total' => $this->resource->posts()->count(),
47+
])
48+
->asCollection(),
4649

4750
'comments' => $this->many(CommentResource::class)
4851
->whenLoaded()
4952
->links(fn() => [
5053
'self' => "https://api.example.com/user/{$this->resource->id}/relationships/comments",
5154
'related' => "https://api.example.com/user/{$this->resource->id}/comments",
55+
])
56+
->meta(fn() => [
57+
'total' => $this->resource->comments()->count(),
5258
]),
5359
];
5460
}

0 commit comments

Comments
 (0)