简体   繁体   English

升级到Laravel 5.8后如何修复Carbon时区

[英]How to fix Carbon timezone after upgrading to Laravel 5.8

After upgrading to Laravel 5.8 the "created_at" field is returning a string like this 升级到Laravel 5.8后,“ created_at”字段将返回这样的字符串

"createdAt": "2019-05-01T16:36:25.000000Z" “ createdAt”:“ 2019-05-01T16:36:25.000000Z”

But I want to return an object like before in Laravel 5.7 like this: 但是我想像这样在Laravel 5.7中返回一个对象,像这样:

"createdAt": { "date": "2019-05-01 19:36:25.000000", "timezone_type": 3, "timezone": "Asia/Baghdad" } “ createdAt”:{“ date”:“ 2019-05-01 19:36:25.000000”,“ timezone_type”:3,“ timezone”:“ Asia / Baghdad”}

I did look around the web found nothing about it and there's nothing about it too in upgrade guide too. 我确实在网上看了一眼,但在升级指南中也一无所获。 I'm returning it from resources just like that a JSON: 我从资源返回它,就像一个JSON:

public function toArray($request)
{
    return [
        'id' => $this->id,
        'createdAt' => $this->created_at,
    ];
}

That's how I could solve it: 这就是我可以解决的方法:

public function toArray($request)
{
    return [
        'id' => $this->id,
        'createdAt' => $this->created_at,
        'date' => Carbon::serializeUsing(function ($createdAt) {
          return [
              'date' => $createdAt->toDateTimeString(),
              'timezone_type' => $createdAt->timezone_type,
              'timezone' => $createdAt->tzName,
          ];
        }),
   ];
}

Now it returns an object like this: 现在,它返回一个像这样的对象:

"createdAt": {
     "date": "2019-05-01 19:36:25",
     "timezone_type": 3,
     "timezone": "Asia/Baghdad"
},
"date": null

The breaking change is documented in the official Carbon documentation . 重大更改记录在正式的Carbon文档中

尝试返回日期的碳实例:

Carbon::parse($this->created_at)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Laravel 5.8 中为西弗吉尼亚设置时区 - How to set timezone in Laravel 5.8 for West Virginia 升级 Laravel 5.8 后,ValidationException 错误消息不起作用 - ValidationException Error Message did not working after Upgrading Laravel 5.8 在 Laravel 中从 5.3 升级到 5.8 后出现未定义的变量错误 - Undefined variable error after upgrading from 5.3 to 5.8 in laravel 将 Laravel 5.8 升级到 6.0 后,缺少 [Route] 所需的参数错误 - Error Missing required parameters for [Route] after upgrading Laravel 5.8 to 6.0 如何修复验证'min' Laravel 5.8 上的错误 - How to fix bug on validation 'min' Laravel 5.8 Laravel 5.8 - 时区环境变量 - Laravel 5.8 - Timezone environment variable Laravel - Carbon:如何比较两个没有时区的日期(字符串)? - Laravel - Carbon: how compare two dates (string) without timezone? Laravel Carbon 如何在不改变小时的情况下改变时区 - Laravel Carbon how to change timezone without changing the hour 如何修复PHP Parse错误:Laravel 5.8上的语法错误,意外的'?' - How to fix PHP Parse error: syntax error, unexpected '?', on laravel 5.8 如何在Laravel 5.8中的twillio API SMS验证中修复客户端错误? - How to fix client error in twillio API SMS Verification in Laravel 5.8?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM