Laravel/Carbon Timestamp 0000-00-00 00:00:00 或发现意外数据。发现意外数据。数据缺失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25484933/
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
Laravel/Carbon Timestamp 0000-00-00 00:00:00 or Unexpected data found. Unexpected data found. Data missing
提问by T2theC
I have a timestamp in the DB and am trying to get the swine to save. I either get an issue where the MySQL
timestamp is blank or I get an error. The format posted is UK standard.
我在数据库中有一个时间戳,我正试图让猪保存。我要么遇到MySQL
时间戳为空的问题,要么遇到错误。发布的格式是英国标准。
Posting data, I have tried some of the following:
发布数据,我尝试了以下一些方法:
$user['crb_date'] = Carbon::createFromFormat('d/m/Y', $data['crb_date']);
$user['crb_date'] = Carbon::createFromFormat('d/m/Y', $data['crb_date'])->toDateTimeString();
// Plus many other more std date(), strtodate() combos
On my Model, I have custom getDates which will convert the dates to Carbon:
在我的模型上,我有自定义的 getDates 将日期转换为 Carbon:
public function getDates()
{
return [
'last_login',
'created_at',
'updated_at',
'dob',
'crb_date'
];
}
Now, if I update the user with the carbon date, I get the following issue:
现在,如果我用碳日期更新用户,我会遇到以下问题:
Unexpected data found. Unexpected data found. Data missing
Unexpected data found. Unexpected data found. Data missing
However, if I dd($user['crb_date']
after the second Carbon date conversion I get
但是,如果我dd($user['crb_date']
在第二次碳日期转换后得到
string '2011-05-01 11:20:23' (length=19)
string '2011-05-01 11:20:23' (length=19)
Which looks pretty good to me.
这对我来说看起来很不错。
If I remove the accessor on the Model, I get it to post, however, I get the blank timestamp
如果我删除模型上的访问器,我会发布它,但是,我会得到空白时间戳
0000-00-00 00:00:00
0000-00-00 00:00:00
I've also tried putting a mutator on the model to set the date there, but issue is exactly the same.
我还尝试在模型上放置一个 mutator 以在那里设置日期,但问题完全相同。
Any ideas what I can do to get this to work? I think it might be something to do with the accessor causing the issue, but need that to work with the dates better afterwards.
任何想法我能做些什么来让它发挥作用?我认为这可能与导致问题的访问器有关,但需要它之后更好地处理日期。
Many thanks for taking the time to help.
非常感谢您抽出时间提供帮助。
This is the mutator I have tried - It was set on the Model. However, the above code (Carbon::createFormFormat... is currently in my repo:
这是我尝试过的 mutator - 它是在模型上设置的。但是,上面的代码(Carbon::createFormFormat... 目前在我的仓库中:
public function setCrbDateAttribute($value)
{
$this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d-m-Y h:i', $value);
}
The Accessor:
存取器:
public function getDates()
{
return [
'last_login',
'created_at',
'updated_at',
'dob',
'crb_date'
];
}
SORTED!
排序!
Thanks to WereWolf's answer below, I tweaked the Model mutator and it is now working like a charm. Here is what I have now:
多亏了 WereWolf 在下面的回答,我调整了 Model mutator,现在它像魅力一样工作。这是我现在所拥有的:
public function setCrbDateAttribute($value)
{
$this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d/m/Y', $value)->toDateTimeString();
}
采纳答案by The Alpha
The problem is in your date
string, for example, you have this:
问题出在你的date
字符串中,例如,你有这个:
public function setCrbDateAttribute($value)
{
$this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d-m-Y h:i', $value);
}
Now, if there is a date like 10-12-2014
then this error will occur because the hour
and minute
is missing. So you make sure that the date contains all the pars and also make sure that the date string contains -
as a separator not /
.
现在,如果有这样的日期,10-12-2014
则会发生此错误,因为hour
和minute
丢失。因此,您要确保日期包含所有解析,并确保日期字符串包含-
作为分隔符 not /
。
In other words, check the $value
before you use Carbon
and make sure your date string contains exactly the same formatted string you've used in the method.
换句话说,$value
在使用之前检查Carbon
并确保您的日期字符串包含与您在该方法中使用的格式完全相同的字符串。
This also happens in an accessor
method, so check the date value first before you use it in Carbon::createFromFormat()
.
这也发生在accessor
方法中,因此在Carbon::createFromFormat()
.
If you are getting the date from user input then validate the date before using it using date
or date_format:format
rule, check the validation here.
如果您从用户输入中获取日期,然后在使用date
或date_format:format
规则之前验证日期,请在此处检查验证。