laravel Carbon\Carbon::now() 抛出 InvalidArgumentException 并带有消息“尾随数据”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29460292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Carbon\Carbon::now() throws InvalidArgumentException with message 'Trailing data'
提问by Rich Jenks
When running the following in Laravel Artisan Tinker:
在 Laravel Artisan Tinker 中运行以下命令时:
$article = new App\Article;
$article->published_at = Carbon\Carbon::now();
I get this error:
我收到此错误:
InvalidArgumentException with message 'Trailing data'
However, Carbon\Carbon::now()
on it's own returns a Carbon
instance as expected.
但是,Carbon\Carbon::now()
它自己会Carbon
按预期返回一个实例。
published_at
should be mutated into Carbon instance via protected $dates = ['published_at'];
in the model and it's also included in protected $fillable
.
published_at
应该通过protected $dates = ['published_at'];
在模型中变异为 Carbon 实例,并且它也包含在protected $fillable
.
Anyone know what's going on here or how I can resolve?
任何人都知道这里发生了什么或我该如何解决?
EDIT: Same thing happens when ran in a closure in routes, so not specific to Tinker
编辑:在路线中的闭包中运行时会发生同样的事情,因此不特定于 Tinker
EDIT 2: Looks like others are experiencing this: https://laracasts.com/discuss/channels/general-discussion/carboncarbonnow-giving-errorand twice in comments for https://laracasts.com/series/laravel-5-fundamentals/episodes/8
编辑 2:看起来其他人正在经历这个:https: //laracasts.com/discuss/channels/general-discussion/carboncarbonnow-giving-error和两次对https://laracasts.com/series/laravel-5-的评论基本面/剧集/8
EDIT 3: Pretty much exactly the same code as the first example is used in https://laracasts.com/series/laravel-5-fundamentals/episodes/15at 15:10 without error.
编辑 3:与第一个示例几乎完全相同的代码在https://laracasts.com/series/laravel-5-fundamentals/episodes/1515:10使用,没有错误。
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).
编辑 4:交换上述代码的第 2 行以$article->published_at = Carbon::now()->format('Y-m-d');
正常工作,甚至包括存储在数据库中的时间(虽然不确定为什么)。
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.
我猜想“尾随数据”可能是指完整的日期时间太长,但 Laravel 自动对日期时间做了这么多事情(例如,自动转换为 Carbon 实例)而不是这个,这似乎很奇怪。
Usage in Edit 3 would be preferable though!
不过,在 Edit 3 中使用会更可取!
回答by 1N5818
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
我发现你不应该使用createFromFormat
,除非第二个参数$date
也是一个碳对象,但如果不是,它只是一个字符串,你可以使用
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' 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:
'Ymd' 是前端将其解析为表单的方式,但它会进入一个数据库,这是 Carbon 吐出的内容。我得到了同样的错误:
[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\Carbon::createFromFormat('Y-m-d', Object(Carbon\Carbon))
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 对象,因此您可能必须确保在表单上是这种情况,而不是date('Y-m-d')
像在 PHP 中那样。
回答by Gengjun Wu
I am following Laracast tutorial, I encountered the same error. I finally figured out what is wrong with this exception.
我正在关注 Laracast 教程,但遇到了同样的错误。我终于弄清楚这个异常有什么问题。
In the function:
在函数中:
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::createFromFormat('Y-m-d', $date);
}
Noticed that my format for $date is 'Y-m-d'
注意到我的 $date 格式是'Ym-d'
However, in my create.blade.php and edit.blade.php, the form input is:
但是,在我的 create.blade.php 和 edit.blade.php 中,表单输入是:
{!! Form::input('data-date', 'published_at', date('d-m-Y'), ['class' => 'form-control']) !!}
Noticed that my date format is 'd-m-Y'.
注意到我的日期格式是'dm-Y'。
This is the reason why the exception is thrown by Laravel.
这就是 Laravel 抛出异常的原因。
After I made the date format the same as 'Y-m-d'across all files, the exception disappears. I hope this helps.
在所有文件中将日期格式设置为与“Ym-d”相同后,异常消失了。我希望这有帮助。
回答by pbarney
You will get this error if the database is returning a date value with a decimal microtime like this:
如果数据库返回带有十进制微时间的日期值,您将收到此错误,如下所示:
2016-10-06 20:16:23.96034
2016-10-06 20:16:23.96034
Those extra decimal digits are the problem. Remove those and it should work.
那些额外的十进制数字是问题所在。删除那些,它应该可以工作。
回答by AVProgrammer
Working with dates that include the trailing timezone data is corrected by removing that trailing data using an Eloquent accessor:
通过使用 Eloquent 访问器删除尾随数据,可以更正包含尾随时区数据的日期:
public function getTimestampColumnAttribute()
{
return Carbon::createFromFormat($this->dateFormat, strtok($this->attributes['timestamp_column'], '.'));
}
回答by Pablo Papalardo
I always has this problem because environments handler dates in different ways. I Just stopped trailing date with carbon with this..
我总是遇到这个问题,因为环境处理程序以不同的方式约会。我只是停止了与碳的追踪日期与此..
public function getDates()
{
return [];
}
public function getDates()
{
return [];
}
回答by Rong Sir
Just remove this function:
只需删除此功能:
public function setPublishedAtAttribute($date){
$this->attributes['published_at']=Carbon::createFromFormat('Y-m-d',$date);
}
becuase that fix the format for published_at field already...
因为已经修复了published_at 字段的格式...
回答by Sajedul Islam Sajid
Just download the carbon source file and replace into the vendor's carbon directory. That's it... https://github.com/briannesbitt/Carbon/releases
只需下载碳源文件并替换到供应商的碳目录中即可。就是这样...... https://github.com/briannesbitt/Carbon/releases