PostResourceWithOptionalData.php 667 B

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