UnitResource.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. 'automatic_protocol' => (bool) ($this->automatic_protocol ?? true),
  38. 'avatar_url' => $this->avatar_url
  39. ? Storage::temporaryUrl($this->avatar_url, now()->addHours(24))
  40. : null,
  41. 'created_at' => Carbon::parse($this->created_at)->format('Y-m-d'),
  42. 'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d'),
  43. 'city' => $this->whenLoaded('city', fn() => [
  44. 'id' => $this->city->id,
  45. 'name' => $this->city->name,
  46. ]),
  47. 'state' => $this->whenLoaded('state', fn() => [
  48. 'id' => $this->state->id,
  49. 'name' => $this->state->name,
  50. 'code' => $this->state->code,
  51. ]),
  52. 'partners' => $this->whenLoaded('partners', fn() => $this->partners->map(fn($partner) => [
  53. 'id' => $partner->id,
  54. 'name' => $partner->name,
  55. 'cpf' => $partner->cpf,
  56. 'birth_date' => $partner->birth_date?->format('d/m/Y'),
  57. ])),
  58. ];
  59. }
  60. /**
  61. * @param \Illuminate\Database\Eloquent\Collection<Unit> $resource
  62. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection<UnitResource>
  63. */
  64. public static function collection($resource): AnonymousResourceCollection
  65. {
  66. return parent::collection($resource);
  67. }
  68. }