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

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

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(); } 

回答1:

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.



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