Laravel/Carbon Timestamp 0000-00-00 00:00:00 or Unexpected data found. Unexpected data found. Data missing

烂漫一生 提交于 2019-12-05 14:26:24

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!