Laravel / Carbon Timestamp 0000-00-00 00:00:00或发现意外数据。发现意外数据。数据丢失 [英] Laravel/Carbon Timestamp 0000-00-00 00:00:00 or Unexpected data found. Unexpected data found. Data missing

查看:1404
本文介绍了Laravel / Carbon Timestamp 0000-00-00 00:00:00或发现意外数据。发现意外数据。数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DB中有一个时间戳,我试图让猪保存。我得到一个问题,其中 MySQL timestamp是空白的或者我收到一个错误。发布的格式是英国标准。



发布数据后,我尝试了以下一些操作:

  $ user ['crb_date'] = Carbon :: createFromFormat('d / m / Y',$ data ['crb_date']); 

$ user ['crb_date'] = Carbon :: createFromFormat('d / m / Y',$ data ['crb_date']) - > toDateTimeString();

//加上其他更多std date(),strtodate()组合

在我的模型上,我有自定义getDate将把日期转换为Carbon:

  public function getDates()
{
return [
'last_login',
'created_at',
'updated_at',
'dob',
'crb_date'
];
}

现在,如果我用碳日期更新用户,我得到以下问题:



发现意外的数据。发现意外数据。数据缺失



但是,如果我 dd($ user ['crb_date'] 在第二个碳日期转换之后,我得到



string'2011-05-01 11:20:23'(length = 19)



对我来说看起来很不错。



如果我删除了Model上的访问器,我得到它发布,但是,我得到空白时间戳



0000-00-00 00:00:00



我也尝试在模型上放置一个mutator来设置日期,但问题是完全一样的。



任何想法,我可以做什么来让这个工作?我认为这可能是与访问者导致问题有关,但需要更好地处理日期。



非常感谢您花时间帮助。



这是我尝试过的mutator - 它是在Model上设置的,但是上面的代码(Carbon :: createFormFormat ...目前在我的repo:

  public function setCrb DateAttribute($ value)
{
$ this-> attributes ['crb_date'] = \Carbon\Carbon :: createFromFormat('d-m-Y h:i',$ value);
}

访问者:

  public function getDates()
{
return [
'last_login',
'created_at',
'updated_at' ,
'dob',
'crb_date'
];
}






SORTED!



感谢WereWolf在下面的回答,我调整了Model mutator,它现在正像一个魅力一样工作。这是我现在所在:

  public function setCrbDateAttribute($ value)
{
$ this- > attributes ['crb_date'] = \Carbon\Carbon :: createFromFormat('d / m / Y',$ value) - > toDateTimeString();
}


解决方案

code> date string,例如,你有这个:

  public function setCrbDateAttribute $ value)
{
$ this-> attributes ['crb_date'] = \Carbon\Carbon :: createFromFormat('dmY h:i',$ value);
}

现在,如果有一个日期像 12-2014 ,则会出现此错误,因为缺少小时分钟。所以你确定日期包含所有pars,并确保日期字符串包含 - 作为分隔符不 /



换句话说,在使用 Carbon 之前,请检查 $ value / code>,并确保您的日期字符串包含与方法中使用的完全相同的格式化字符串。



这也发生在 accessor 方法,所以在 Carbon :: createFromFormat()中使用它之前先检查日期值。



如果您从用户输入获取日期,则使用 date date_format:format 规则,检查此处的验证


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.

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:

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

However, if I dd($user['crb_date'] after the second Carbon date conversion I get

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

I've also tried putting a mutator on the model to set the date there, but issue is exactly the same.

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:

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:

public function setCrbDateAttribute($value)
{
    $this->attributes['crb_date'] = \Carbon\Carbon::createFromFormat('d/m/Y', $value)->toDateTimeString();
}

解决方案

The problem is in your date string, for example, you have this:

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 /.

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.

This also happens in an accessor method, so check the date value first before you use it in 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.

这篇关于Laravel / Carbon Timestamp 0000-00-00 00:00:00或发现意外数据。发现意外数据。数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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