How To Change Model Default Timestamps Column Names In Laravel

There will be a situation where you’ve to continue using your old or legacy database which might differ with Laravel’s way of naming column names in tables.

Laravel provides few default timestamps fields such as created_at, updated_at and deleted_at. Most of the Laravel developers finds it useful.

But you’ve might have a use case where you need to change these fields and in this tutorial we’re going to see How to change these timestamps column names?

To override these default column names, you need to define constant CREATED_AT or UPDATED_AT, as per your requirement in the model.

If your model uses soft deletes you can override the deleted_at column name using DELETED_AT const.

Let us understand through example, which will make more clearer

Example

class PlayStoreApplication extends Model
{
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'publishedAt';

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

    /**
     * The name of the "deleted at" column.
     *
     * @var string
     */
    const DELETED_AT = 'removedAt';
}

Happy coding

Avatar

A web geek, an industry experienced web developer & tutor/instructor residing in India 🇮🇳

Next
Previous

Related