Posted by Arjun on Wednesday 22nd November 2017

How to change created_at and updated_at column names in Laravel

How to Laravel 5.x created_at updated_at

You can easily change created_at and updated_at column names by overriding CREATED_AT and UPDATED_AT constants in your Eloquent model.

Example #Task Model

Let's assume you have a model called Task and it has columns called createdAt and updatedAt. But Laravel tries to insert data created_at and udpated_at column by default. So in order to inform Laravel about our new columns just define constants in your eloquent model as shown below -

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'createdAt';
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'updatedAt';
}

Now Laravel will use above defined columns instead of created_at and updated_at columns.

Here is Eloquent base model - https://github.com/illuminate/database/blob/5.5/Eloquent/Model.php