PostResourceWithOptionalAppendedAttributes.php 801 B

123456789101112131415161718192021222324
  1. <?php
  2. namespace Illuminate\Tests\Integration\Http\Fixtures;
  3. use Illuminate\Http\Resources\Json\JsonResource;
  4. class PostResourceWithOptionalAppendedAttributes extends JsonResource
  5. {
  6. public function toArray($request)
  7. {
  8. return [
  9. 'id' => $this->id,
  10. 'first' => $this->whenAppended('is_published'),
  11. 'second' => $this->whenAppended('is_published', 'override value'),
  12. 'third' => $this->whenAppended('is_published', function () {
  13. return 'override value';
  14. }),
  15. 'fourth' => $this->whenAppended('is_published', $this->is_published, 'default'),
  16. 'fifth' => $this->whenAppended('is_published', $this->is_published, function () {
  17. return 'default';
  18. }),
  19. ];
  20. }
  21. }