Laravel Change / Override Timestamp Column Names

Suppose, if you're working with a non-Laravel database and your timestamp columns are named differently. In this situation, you need to change the timestamp column names. But in Laravel, there is an awesome feature. You can override the timestamp column names on the Laravel model.

Table of Contents

The Solution

Have a look at the code:

class YourModel extends Eloquent {

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

    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'your_custom_updated_at_field_name';

}

In your model, just set your custom timestamp column names.

An Example

Suppose, we are migrating a WordPress application to Laravel. WordPress posts table's timestamp column names are post_date and post_modified.

Now in your mode, override timestamp column names like:

class Post extends Model
{
    use HasFactory;

    // override timestamp column names
    const CREATED_AT = 'post_date';
    const UPDATED_AT = 'post_modified';
}

Video

You can take a look at the video too:

That's all. Thank you.