dimanche 15 avril 2018

Carbon - Unexpected data found. Traling data

There are numerous questions on this topic, and the closest I can find it this one (based on Laravel 4 though).

I am using Laravel, with Vue on the front-end and am having trouble saving data from an AJAX call (from axios).

The stack trace below leads me to believe it is related to date serializing:

[2018-04-16 00:56:53] local.ERROR: Unexpected data found. Trailing data {"userId":8084,"email":"james@example.com","exception":"[object] (InvalidArgumentException(code: 0): Unexpected data found. Trailing data at C:\xampp\htdocs\community-tools\vendor\ esbot\carbon\src\Carbon\Carbon.php:733) [stacktrace]

0 C:\xampp\htdocs\community-tools\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(738): Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2018-04-16T00:4...')

1 C:\xampp\htdocs\community-tools\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(761): Illuminate\Database\Eloquent\Model->asDateTime('2018-04-16T00:4...')

2 C:\xampp\htdocs\community-tools\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Concerns\HasAttributes.php(555): Illuminate\Database\Eloquent\Model->fromDateTime('2018-04-16T00:4...')

3 C:\xampp\htdocs\community-tools\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(231): Illuminate\Database\Eloquent\Model->setAttribute('created_at', '2018-04-16T00:4...')

I assume that is because of this; Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2018-04-16T00:4...') it is trying to create a date from the wrong format...

I haven't set anything in my models to do with dates, but have set this in my AppServiceProvider.php:

public function boot()
{
    Carbon::serializeUsing(function ($carbon) {
        return $carbon->format('Y-m-d\TH:i:s.u\Z');
    });
}

As per the Laravel docs on date serialization.

This error is occurring when trying to save a model.

What more do I need to do to get this working?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire