Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cparrish817's avatar

Date Mutators

I'm pulling data from an API - the created_at and updated_at fields are of the format.

'created_at' => '2015-07-28T23:10:30Z', 'updated_at' => '2015-07-29T00:22:06Z'

I'm trying to push the data into my database but am getting the following error:

InvalidArgumentException in Carbon.php line 414:
Unexpected data found.
Trailing data

I'm thinking I need a date Mutator on my migration but not sure how to go about that for pushing the data to the database. my migration looks like this.

    public function up()
    {
        Schema::create('commits', function (Blueprint $table) {
            $table->increments('id');
            $table->text('number');
            $table->string('userName');
            $table->string('title');
            $table->text('body');
            $table->string('sha')->unique();
            $table->dateTime('created_at');
            $table->dateTime('updated_at');
        });
    }

What do I need to do for Carbon to recognize my format? Or am I making the wrong assumtion?

0 likes
6 replies
jbowman99's avatar

$table->timestamps();

created_at and updated_at come stock with the timestamps(); function instead of $table->dateTime('created_at'); $table->dateTime('updated_at');

just put

$table->timestamps();

you'll have access to the data like normal if you need to display it

bobbybouwmann's avatar

You can always create a Carbon object and insert that, when using the timestamps fields

$date  = Carbon::createFromFormat('your date formate here');
$date = new Carbon('2015-07-28T23:10:30Z');

$timestamps = $date->timestamp
$dateTimeFormat = $date->format('Y-m-d H:i:s');

Source: http://carbon.nesbot.com/docs/

cparrish817's avatar

jbowman99 So far I'm seeing the same error.

public function up()
    {
        Schema::create('commits', function (Blueprint $table) {
            $table->increments('id');
            $table->text('number');
            $table->string('userName');
            $table->string('title');
            $table->text('body');
            $table->string('sha')->unique();
            $table->timestamp('created_at');
            $table->timestamp('updated_at');
        });
    }
cparrish817's avatar

Same with:

  public function up()
    {
        Schema::create('commits', function (Blueprint $table) {
            $table->increments('id');
            $table->text('number');
            $table->string('userName');
            $table->string('title');
            $table->text('body');
            $table->string('sha')->unique();
            $table->timestamps('');
        });
    }
squigg's avatar
squigg
Best Answer
Level 2

I can't see how this has anything to do with your database - it's PHP that's throwing the error, not the DB. It would be unusual for the data from your API to go straight into the database without going through an intermediate Model.

Carbon should accept that date format, so try using mutator functions on your model, and converting that string into a Carbon object before saving into the attributes, using something like:

function setCreatedAtAttribute($date) {
    $this->attributes['created_at'] = new Carbon($date);
}
function setUpdatedAtAttribute($date) {
    $this->attributes['updated_at'] = new Carbon($date);
}
1 like

Please or to participate in this conversation.