UnitPartnerResource.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Http\Resources;
  3. use Carbon\Carbon;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Http\Resources\Json\JsonResource;
  6. use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
  7. use Illuminate\Support\Facades\Storage;
  8. use App\Models\UnitPartner;
  9. class UnitPartnerResource extends JsonResource
  10. {
  11. public function toArray(Request $request): array
  12. {
  13. return [
  14. 'id' => $this->id,
  15. 'unit_id' => $this->unit_id,
  16. 'name' => $this->name,
  17. 'social_name' => $this->social_name,
  18. 'role' => $this->role,
  19. 'cpf' => $this->cpf,
  20. 'rg' => $this->rg,
  21. 'birth_date' => $this->birth_date?->format('Y-m-d'),
  22. 'participation' => $this->participation,
  23. 'email' => $this->email,
  24. 'secondary_email' => $this->secondary_email,
  25. 'phone_number' => $this->phone_number,
  26. 'cell_number' => $this->cell_number,
  27. 'postal_code' => $this->postal_code,
  28. 'street' => $this->street,
  29. 'address_number' => $this->address_number,
  30. 'neighborhood' => $this->neighborhood,
  31. 'complement' => $this->complement,
  32. 'city_id' => $this->city_id,
  33. 'state_id' => $this->state_id,
  34. 'avatar_url' => $this->avatar_url
  35. ? Storage::temporaryUrl($this->avatar_url, now()->addHours(24))
  36. : null,
  37. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
  38. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
  39. 'city' => $this->whenLoaded('city', fn() => [
  40. 'id' => $this->city->id,
  41. 'name' => $this->city->name,
  42. ]),
  43. 'state' => $this->whenLoaded('state', fn() => [
  44. 'id' => $this->state->id,
  45. 'name' => $this->state->name,
  46. 'code' => $this->state->code,
  47. ]),
  48. ];
  49. }
  50. /**
  51. * @param \Illuminate\Database\Eloquent\Collection<UnitPartner> $resource
  52. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<UnitPartnerResource>
  53. */
  54. public static function collection($resource): AnonymousResourceCollection
  55. {
  56. return parent::collection($resource);
  57. }
  58. }