【发布时间】:2021-10-30 01:47:51
【问题描述】:
我在 baseModel 中定义了创建格式,如下所示:
protected function serializeDate(DateTimeInterface $date): string
{
return Carbon::instance($date)->toIso8601String();
}
但是当我使用 api 资源时,改变了 created_at 格式
这是 api 资源:
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')),
];
}
为什么要更改所有 api 资源的格式
【问题讨论】:
-
你使用的是什么版本的 Laravel?和 ?请编辑您的问题并向我们展示您的通话方式
toArray。 -
下面的帖子回答了你的问题吗?
-
是的,但不推荐使用“setToStringFormat”。有没有其他选择?