Change name of Laravel's created_at and updated_at

PhpMysqlLaravelLaravel 4

Php Problem Overview


Can I map Laravel's timestamps from:

created_at to post_date and post_date_gmt?

updated_at to post_modified and post_modified_gmt?


I'm migrating a Wordpress application to Laravel.

I'm accessing a copy of the Wordpress database using Laravel. The live version of the database is still in use so I don't want to change the schema.

The Posts table has post_date, post_date_gmt, post_modified and post_modified_gmt, but Laravel is expecting created_at and updated_at.

Is there anyway to change the column names that Laravel looks for?

I'd like Laravel to update the timestamps of all the columns that are already there.

Php Solutions


Solution 1 - Php

The accepted answer may cause problems with updating timestamps unfortunately.

You'd better override consts on your model:

const CREATED_AT = 'post_date';
const UPDATED_AT = 'post_modified';

then methods getCreatedAtColumn and getUpdatedAtColumn will return post_date and post_modified respectively, but won't do any harm.

For the other columns you need use events like @Oni suggested.

Solution 2 - Php

If you look into the source of the Eloquent class

https://github.com/illuminate/database/blob/4.2/Eloquent/Model.php#L223-L235

You should be able to change these column names pretty easily by overriding those constants.

<?php

class YourModel extends Eloquent {

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

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

}

As for the _gmt version of those timestamps, you might want to look into events. Here is a good start

http://driesvints.com/blog/using-laravel-4-model-events

Solution 3 - Php

Just put this in your tables model file,

 const CREATED_AT = 'your_custom_created_at_field_name';
 const UPDATED_AT = 'your_custom_updated_at_field_name';

My model looks like this for avoiding confusions

class diarymodule extends Model
{
    protected $table = 'diarymodule';
    const CREATED_AT = 'CreatedDate';
    const UPDATED_AT = 'UpdatedDate';
}

Solution 4 - Php

You could try using the attribute getters and getters:

class Post extends Eloquent
{
    public function getCreatedAtAttribute()
    {
        return $this->attributes['post_date'];
    }

    public function setCreatedAtAttribute($value)
    {
        $this->attributes['post_date'] = $value;

        // this may not work, depends if it's a Carbon instance, and may also break the above - you may have to clone the instance
        $this->attributes['post_date_gmt'] = $value->setTimezone('UTC');
    }
}

I don't know if this will work, but you could give it a go. Certainly a base to go on maybe - you'll have to play around with it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1894292View Question on Stackoverflow
Solution 1 - PhpJarek TkaczykView Answer on Stackoverflow
Solution 2 - Phpuser3774430View Answer on Stackoverflow
Solution 3 - PhpShamseer AhammedView Answer on Stackoverflow
Solution 4 - PhpalexrussellView Answer on Stackoverflow