Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Ensure relation names are properly "snaked" in JsonResource::whenCounted() method #43322

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function whenCounted($relationship, $value = null, $default = null)
$default = new MissingValue();
}

$attribute = Str::finish($relationship, '_count');
$attribute = (string) Str::of($relationship)->snake()->finish('_count');

if (! isset($this->resource->getAttributes()[$attribute])) {
return value($default);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function toArray($request)
return [
'id' => $this->id,
'authors' => $this->whenCounted('authors_count'),
'favourite_posts' => $this->whenCounted('favouritedPosts'),
'comments' => $this->whenCounted('comments', function ($count) {
return "$count comments";
}, 'None'),
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public function testResourcesMayLoadOptionalRelationshipCounts()
'title' => 'Test Title',
'authors_count' => 2,
'comments_count' => 5,
'favourited_posts_count' => 1,
]);

return new PostResourceWithOptionalRelationshipCounts($post);
Expand All @@ -332,6 +333,7 @@ public function testResourcesMayLoadOptionalRelationshipCounts()
'data' => [
'id' => 5,
'authors' => 2,
'favourite_posts' => 1,
'comments' => '5 comments',
],
]);
Expand Down