| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace App\Http\Resources;
- use Illuminate\Http\Request;
- use Illuminate\Http\Resources\Json\JsonResource;
- class FormDataResource extends JsonResource
- {
- /**
- * Transform the resource into an array.
- *
- * @return array<string, mixed>
- */
- public function toArray(Request $request): array
- {
- return [
- 'id' => $this->id,
- 'uuid' => $this->uuid,
- 'name' => $this->name,
- 'email' => $this->email,
- 'phone' => $this->phone,
- 'gender' => $this->gender ? 'Male' : 'Female',
- 'message' => $this->message,
- 'submitted_at' => $this->submitted_at->format('Y-m-d H:i:s'),
- 'created_at' => $this->created_at->format('Y-m-d H:i:s'),
- 'updated_at' => $this->updated_at->format('Y-m-d H:i:s'),
- 'category' => $this->whenLoaded('category', function () {
- return [
- 'id' => $this->category->id,
- 'name' => $this->category->name,
- ];
- }),
- 'comments' => $this->whenLoaded('comments', function () {
- return $this->comments->map(function ($comment) {
- return [
- 'id' => $comment->id,
- 'content' => $comment->content,
- 'user' => $comment->user ? $comment->user->name : 'Anonymous',
- ];
- });
- }),
- 'links' => [
- 'self' => route('api.form-data.show', $this->id),
- 'edit' => route('api.form-data.update', $this->id),
- 'delete' => route('api.form-data.destroy', $this->id),
- ],
- ];
- }
- }
|