Skip to content

Commit

Permalink
Ensure relations are properly snaked (#43322)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman authored Jul 20, 2022
1 parent d2caa9b commit c8d3851
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 1 deletion.
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

0 comments on commit c8d3851

Please sign in to comment.