-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Add
whenCounted
to JsonResource (#43101)
* Add ability to conditionally include a relation's count * Pass count into value callback and allow specifying count in relation name * Add tests * CS fix * CS fix * Remove `hasAttribute` method from `Model` * Change comparison order to follow suit with other methods
- Loading branch information
1 parent
a105ae5
commit 1b09251
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/Integration/Http/Fixtures/PostResourceWithOptionalRelationshipCounts.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Http\Fixtures; | ||
|
||
class PostResourceWithOptionalRelationshipCounts extends PostResource | ||
{ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'authors' => $this->whenCounted('authors_count'), | ||
'comments' => $this->whenCounted('comments', function ($count) { | ||
return "$count comments"; | ||
}, 'None'), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters