Carbon\Carbon::now() throws InvalidArgumentException with message 'Trailing data'
Asked Answered
B

8

8

When running the following 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's own returns a Carbon instance as expected.

published_at should be mutated into Carbon instance via protected $dates = ['published_at']; in the model and it's also included in protected $fillable.

Anyone know what's going on here or how I can resolve?


EDIT: Same thing happens when ran in a closure in routes, so not specific to Tinker

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

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

EDIT 4: Swapping line 2 of the above code to $article->published_at = Carbon::now()->format('Y-m-d'); works fine and even includes time when stored in the database (although not sure why).

I'd guess that "trailing data" could refer to the full datetime being too long, but seems strange that Laravel does so much with datetimes automatically (auto-converting to Carbon instances, for example) but not this.

Usage in Edit 3 would be preferable though!

Burgas answered 5/4, 2015 at 17:44 Comment(6)
Looks like a regular DateTime error, however it shouldn't happen with now() method. Is there any chance something is wrong with your timezone settings? What does the timezone configuration look like in config/app.php?Monicamonie
Timezone is default:'timezone' => 'UTC',Burgas
By looking through the source code, it looks like it doesn't get past this line return new DateTimeZone(date_default_timezone_get()); during class construction. If that doesn't lead you anywhere I doubt there is anything you can do except using native date functions.Monicamonie
Interesting — calling that separately works fineBurgas
Could it already be a Carbon instance?Behre
Does this answer your question? Laravel model Trailing Data when save the modelAutum
G
2

I've found that you should should not use createFromFormat, unless the second paramater $date is also a Carbon object, but if it's not and it's just a string you can just use

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

I think there's a little more overhead with regards to it having to figure out what format it's in, but this was my temporary workaround.

'Y-m-d' is the way the front parsed it into the form, but it's going into a database which is what Carbon spits 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('Y-m-d', Object(Carbon\Carbon))

indicates that the second parameter must be a Carbon object, so you may have to make sure that is the case on the form rather than just date('Y-m-d') as you would in PHP.

Gouge answered 16/8, 2015 at 23:23 Comment(0)
A
1

I am following Laracast tutorial, I encountered the same error. I finally figured out what is wrong with this exception.

In the function:

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

Noticed that my format for $date is 'Y-m-d'

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

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

Noticed that my date format is 'd-m-Y'.

This is the reason why the exception is thrown by Laravel.

After I made the date format the same as 'Y-m-d' across all files, the exception disappears. I hope this helps.

Albumen answered 3/3, 2016 at 2:8 Comment(0)
F
1

You will get this error if the database is returning a date value with a decimal microtime like this:

2016-10-06 20:16:23.96034

Those extra decimal digits are the problem. Remove those and it should work.

Fungal answered 13/10, 2016 at 1:34 Comment(0)
A
0

Working with dates that include the trailing timezone data is corrected by removing that trailing data using an Eloquent accessor:

public function getTimestampColumnAttribute()
{
    return Carbon::createFromFormat($this->dateFormat, strtok($this->attributes['timestamp_column'], '.'));
}
Aquiver answered 8/5, 2018 at 15:31 Comment(0)
B
0

I always has this problem because environments handler dates in different ways. I Just stopped trailing date with carbon with this..

public function getDates() { return []; }

Berkey answered 21/11, 2018 at 13:44 Comment(0)
N
0

Just Use in model

protected $dateFormat = ' H:i:s.Y-m-du';

or else

 public function getDateFormat()
 {
     return 'Y-m-d H:i:s.u';
 }

Negative answered 7/7, 2021 at 14:14 Comment(0)
C
-1

Just remove this function:

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

becuase that fix the format for published_at field already...

Cresset answered 10/5, 2015 at 10:48 Comment(0)
I
-4

Just download the carbon source file and replace into the vendor's carbon directory. That's it... https://github.com/briannesbitt/Carbon/releases

Ingres answered 15/7, 2018 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.