UnitResource.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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\Unit;
  9. class UnitResource extends JsonResource
  10. {
  11. /**
  12. * Transform the resource into an array.
  13. *
  14. * @return array<string, mixed>
  15. */
  16. public function toArray(Request $request): array
  17. {
  18. return [
  19. 'id' => $this->id,
  20. 'name' => $this->name,
  21. 'fantasy_name' => $this->fantasy_name,
  22. 'social_reason' => $this->social_reason,
  23. 'cnpj' => $this->cnpj,
  24. 'state_registration' => $this->state_registration,
  25. 'name_responsible' => $this->name_responsible,
  26. 'street' => $this->street,
  27. 'address_number' => $this->address_number,
  28. 'postal_code' => $this->postal_code,
  29. 'neighborhood' => $this->neighborhood,
  30. 'complement' => $this->complement,
  31. 'city_id' => $this->city_id,
  32. 'state_id' => $this->state_id,
  33. 'email' => $this->email,
  34. 'secondary_email' => $this->secondary_email,
  35. 'phone_number' => $this->phone_number,
  36. 'cell_number' => $this->cell_number,
  37. 'avatar_url' => $this->avatar_url
  38. ? Storage::temporaryUrl($this->avatar_url, now()->addHours(24))
  39. : null,
  40. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d'),
  41. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d'),
  42. 'city' => $this->whenLoaded('city', fn() => [
  43. 'id' => $this->city->id,
  44. 'name' => $this->city->name,
  45. ]),
  46. 'state' => $this->whenLoaded('state', fn() => [
  47. 'id' => $this->state->id,
  48. 'name' => $this->state->name,
  49. 'code' => $this->state->code,
  50. ]),
  51. 'partners' => $this->whenLoaded('partners', fn() => $this->partners->map(fn($partner) => [
  52. 'id' => $partner->id,
  53. 'name' => $partner->name,
  54. 'cpf' => $partner->cpf,
  55. 'birth_date' => $partner->birth_date?->format('d/m/Y'),
  56. ])),
  57. ];
  58. }
  59. /**
  60. * @param \Illuminate\Database\Eloquent\Collection<Unit> $resource
  61. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<UnitResource>
  62. */
  63. public static function collection($resource): AnonymousResourceCollection
  64. {
  65. return parent::collection($resource);
  66. }
  67. }