Carbon \ Carbon :: now () throws an InvalidArgumentException with the message "Trailing data"

When launched in 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 returns an instance of Carbon , as expected.

published_at must be mutated into a Carbon instance via protected $dates = ['published_at']; in the model, and also included in protected $fillable .

Does anyone know what is going on here or how can I decide?


EDIT: The same thing happens when you run in closures in routes, so it doesn't apply to Tinker

EDIT 2: It looks like others are experiencing this: https://laracasts.com/discuss/channels/general-discussion/carboncarbonnow-giving-error and twice in the comments for https://laracasts.com/series/laravel-5- fundamentals / episodes / 8

EDIT 3: Pretty much the exact same code as the first example is used at https://laracasts.com/series/laravel-5-fundamentals/episodes/15 at 15:10 without errors.

EDIT 4: Switch line 2 of the above code to $article->published_at = Carbon::now()->format('Ym-d'); It works great and even includes the time when it is stored in the database (although it is not clear why).

I would suggest that the β€œfinal data” may refer to the fact that the total datetime is too long, but it seems strange that Laravel does so much with datetimes automatically (for example, it automatically converts to Carbon instances), but not that.

Use in Edit 3 would be preferable though!

+6
source share
4 answers

I found that you should not use createFromFormat if the second parameter $date also not a Carbon object, but if it is not, and it is just a string, you can simply use

 public function setPublishedAtAttribute($date){ $this->attributes['published_at'] = Carbon::parse($date); } 

I think that there is a bit more overhead due to the fact that he needs to find out what format he is in, but this was my temporary solution.

'Ymd' is how the front parsed it in the form, but it gets into the database from which Carbon breaks out. I got the same error:

 [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('Ym-d', Object(Carbon\Carbon))

indicates that the second parameter should be a Carbon object, so you might need to make sure this is a case in the form, not just date('Ym-d') , as in PHP.

+2
source

I follow the Laracast manual, I encountered the same error. I finally realized what was wrong with this exception.

In function:

 public function setPublishedAtAttribute($date) { $this->attributes['published_at'] = Carbon::createFromFormat('Ym-d', $date); } 

Note that my format for $ date is 'Ym-d'

However, in my create.blade.php and edit.blade.php files, the form input is:

 {!! Form::input('data-date', 'published_at', date('dm-Y'), ['class' => 'form-control']) !!} 

It is noted that my date format is 'dm-Y' .

This is why the exception is thrown by Laravel.

After I made the date format the same as 'Ym-d' for all files, the exception will disappear. Hope this helps.

+1
source

You will get this error if the database returns a date value with decimal micro-time as follows:

2016-10-06 20:16:23.96034

These extra decimal digits are a problem. Remove those and they should work.

0
source

Just remove this function:

 public function setPublishedAtAttribute($date){ $this->attributes['published_at']=Carbon::createFromFormat('Ym-d',$date); } 

because they already fix the format for publish_at ...

-1
source

Source: https://habr.com/ru/post/984709/


All Articles