FormDataResource.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. class FormDataResource extends JsonResource
  6. {
  7. /**
  8. * Transform the resource into an array.
  9. *
  10. * @return array<string, mixed>
  11. */
  12. public function toArray(Request $request): array
  13. {
  14. return [
  15. 'id' => $this->id,
  16. 'uuid' => $this->uuid,
  17. 'name' => $this->name,
  18. 'email' => $this->email,
  19. 'phone' => $this->phone,
  20. 'gender' => $this->gender ? 'Male' : 'Female',
  21. 'message' => $this->message,
  22. 'submitted_at' => $this->submitted_at->format('Y-m-d H:i:s'),
  23. 'created_at' => $this->created_at->format('Y-m-d H:i:s'),
  24. 'updated_at' => $this->updated_at->format('Y-m-d H:i:s'),
  25. 'category' => $this->whenLoaded('category', function () {
  26. return [
  27. 'id' => $this->category->id,
  28. 'name' => $this->category->name,
  29. ];
  30. }),
  31. 'comments' => $this->whenLoaded('comments', function () {
  32. return $this->comments->map(function ($comment) {
  33. return [
  34. 'id' => $comment->id,
  35. 'content' => $comment->content,
  36. 'user' => $comment->user ? $comment->user->name : 'Anonymous',
  37. ];
  38. });
  39. }),
  40. 'links' => [
  41. 'self' => route('api.form-data.show', $this->id),
  42. 'edit' => route('api.form-data.update', $this->id),
  43. 'delete' => route('api.form-data.destroy', $this->id),
  44. ],
  45. ];
  46. }
  47. }