0

i defined created at format in baseModel like this:

protected function serializeDate(DateTimeInterface $date): string
{
    return Carbon::instance($date)->toIso8601String();
}

but when i use api Resource, changed created_at format

this is api resource:

 public function toArray($request)
{
    return [
        'id' => $this->id,
        'last_status' => $this->lastStatus->status,
        'terminal' => [
            'provider' => $this->terminal_provider,
            'psp' => $this->terminal_psp,
        ],
        'account' => [
            'iban' => $this->account_iban,
        ],
        'count' => $this->count,
        'amount_settle' => $this->amount_settle,
        'amount_wage' => $this->amount_settle,
        'created_at' => $this->created_at,
        'statuses' => AggregateStatusResource::collection($this->whenLoaded('statuses')),
        'shaparak_files' => ShaparakFileResource::collection($this->whenLoaded('shaparakFiles')),
    ];
}

whay change format for all of api resources

mohammad
  • 95
  • 7

1 Answers1

1

The created_at date in your Resource class is not getting converted using toIso8601String because it's not longer using the model's toArray() method which will call serializeDate().

One option would be to update your Resource to explicitly format the created_at date:

'created_at' => $this->created_at->toIso8601String(),
Rwd
  • 34,180
  • 6
  • 64
  • 78