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

brocktaylor87's avatar

Why are CREATED_AT and UPDATED_AT Constants in the Eloquent Model class?

I'm in the process of migrating an existing application over to laravel and I ran into problems with the created_at and updated_at tables. The database that I'm currently using already has these fields, but they're named differently. I can override $primaryKey, but not the timestamp fields. Is there a purpose for this?

here's the code in Illuminate\Database\Eloquent\Model.php

    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'created_at';

    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'updated_at';
0 likes
3 replies
RachidLaasri's avatar

Just migrate your database, and add $dates property to your Model.

protected $dates = ['custom_created_at_field', 'custom_updated_at_field'];
kkiernan's avatar

Is this what you mean?

By default, Eloquent expects created_at and updated_at columns to exist on your tables. If you do not wish to have these columns automatically managed by Eloquent, set the $timestamps property on your model to false:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;
}

http://laravel.com/docs/5.1/eloquent#eloquent-model-conventions

1 like

Please or to participate in this conversation.