Carbon \ Carbon :: now()引发InvalidArgumentException与消息“跟踪数据" [英] Carbon\Carbon::now() throws InvalidArgumentException with message 'Trailing data'

查看:75
本文介绍了Carbon \ Carbon :: now()引发InvalidArgumentException与消息“跟踪数据"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel Artisan Tinker中运行以下命令时:

When running the following in Laravel Artisan Tinker:

$article = new App\Article;
$article->published_at = Carbon\Carbon::now();

我收到此错误:

InvalidArgumentException with message 'Trailing data'

但是,单独的 Carbon \ Carbon :: now()会按预期返回 Carbon 实例.

However, Carbon\Carbon::now() on it's own returns a Carbon instance as expected.

published_at 应该通过模型中的 protected $ dates = ['published_at']; 突变为Carbon实例,并且也应包含在 protected $ fillable .

published_at should be mutated into Carbon instance via protected $dates = ['published_at']; in the model and it's also included in protected $fillable.

任何人都知道这里发生了什么或我如何解决?

Anyone know what's going on here or how I can resolve?

在路线的封闭路线中运行时也会发生同样的事情,因此并非专门针对Tinker

Same thing happens when ran in a closure in routes, so not specific to Tinker

看起来其他人正在经历这种情况: https://laracasts.com/discuss/channels/general-discussion/carboncarbonnow-giving-error ,并在两次评论

EDIT 2: Looks like others are experiencing this: https://laracasts.com/discuss/channels/general-discussion/carboncarbonnow-giving-error and twice in comments for https://laracasts.com/series/laravel-5-fundamentals/episodes/8

https://laracasts.com/series/laravel-5-fundamentals/episodes/15 在15:10正确无误.

EDIT 3: Pretty much exactly the same code as the first example is used in https://laracasts.com/series/laravel-5-fundamentals/episodes/15 at 15:10 without error.

将上述代码的第2行交换为 $ article-> published_at = Carbon :: now()-> format('Ym-d'); 效果很好,甚至包括存储在数据库中的时间(尽管不确定原因).

EDIT 4: Swapping line 2 of the above code to $article->published_at = Carbon::now()->format('Y-m-d'); works fine and even includes time when stored in the database (although not sure why).

我猜想跟踪数据"可能指的是整个日期时间太长,但是对于Laravel在自动设置日期时间方面做了很多事情(例如,自动转换为Carbon实例)却感到奇怪,但这并不奇怪.

I'd guess that "trailing data" could refer to the full datetime being too long, but seems strange that Laravel does so much with datetimes automatically (auto-converting to Carbon instances, for example) but not this.

最好使用Edit 3!

Usage in Edit 3 would be preferable though!

推荐答案

我发现您不应该使用 createFromFormat ,除非第二个参数 $ date 是也是Carbon对象,但如果不是,而只是一个字符串,则可以使用

I've found that you should should not use createFromFormat, unless the second paramater $date is also a Carbon object, but if it's not and it's just a string you can just use

public function setPublishedAtAttribute($date){
    $this->attributes['published_at'] = Carbon::parse($date);
}

我认为必须弄清楚它采用的格式要花一些时间,但这是我的临时解决方法.

I think there's a little more overhead with regards to it having to figure out what format it's in, but this was my temporary workaround.

'Y-m-d'是前端将其解析为表单的方式,但它正在进入数据库,这就是Carbon所发出的内容.我遇到了同样的错误:

'Y-m-d' is the way the front parsed it into the form, but it's going into a database which is what Carbon spits out. I got the same error:

[2015-08-16 21:35:57] production.ERROR: exception 'InvalidArgumentException' with message 'Trailing data' in /Users/alexanderkleinhans/laravel/vendor/nesbot/carbon/src/Carbon/Carbon     .php:414

我相信堆栈跟踪的第一部分

I believe in the first part of the stack trace,

Carbon \ Carbon :: createFromFormat('Y-m-d',Object(Carbon \ Carbon))

表示第二个参数必须是Carbon对象,因此您可能必须确保表单上是这种情况,而不仅仅是 date('Ym-d')PHP.

indicates that the second parameter must be a Carbon object, so you may have to make sure that is the case on the form rather than just date('Y-m-d') as you would in PHP.

这篇关于Carbon \ Carbon :: now()引发InvalidArgumentException与消息“跟踪数据"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆