Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] Conditionally returning appended attributes in API resources (#…
…33422) * Add hasAppended method to HasAttributes trait * Add whenAppended() method for resources * Fix docblock * Test hasAppended() method * Test whenAppended() method * Fix docblock * Improve test * Test the unhappy path
- Loading branch information
1 parent
f1a21b3
commit 9c29794
Showing
6 changed files
with
121 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
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
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
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
24 changes: 24 additions & 0 deletions
24
tests/Integration/Http/Fixtures/PostResourceWithOptionalAppendedAttributes.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,24 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Http\Fixtures; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class PostResourceWithOptionalAppendedAttributes extends JsonResource | ||
{ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'first' => $this->whenAppended('is_published'), | ||
'second' => $this->whenAppended('is_published', 'override value'), | ||
'third' => $this->whenAppended('is_published', function () { | ||
return 'override value'; | ||
}), | ||
'fourth' => $this->whenAppended('is_published', $this->is_published, 'default'), | ||
'fifth' => $this->whenAppended('is_published', $this->is_published, function () { | ||
return 'default'; | ||
}), | ||
]; | ||
} | ||
} |
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