59

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.

Laurence
  • 58,936
  • 21
  • 171
  • 212
user1894292
  • 829
  • 1
  • 8
  • 19

4 Answers4

93

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.

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • Can you explain how the accepted answer may cause problems? I'm intrigued. – alexrussell Jun 25 '14 at 10:00
  • 1
    Eloquent Model class refers to `static::CREATED_AT` and `static::UPDATED_AT` in some methods, also in that particular 2 that @Oni overrides. So hardcoding the columns in those methods would make Eloquent search for `created_at` and `updated_at` anyway when updating timestamps (`updateTimestamps` method for example). The same goes for `DELETED_AT` when soft deletes are used. – Jarek Tkaczyk Jun 25 '14 at 10:03
  • Hmm that seems like a possible bug in Laravel (in that it should be using the methods not the constants), right? – alexrussell Jun 25 '14 at 11:24
  • Definitely! At least big inconsistency, but there are more in Eloquent. Still it's by far the easiest ORM to learn and really functional component of the framework. – Jarek Tkaczyk Jun 25 '14 at 11:30
  • At least that's what that comes into my mind (overriding methods instead of constants) when I try to whip up the code example. But, it does come with a consequence of having unexpected behaviour. Overriding constants somewhat felt like a hack to me but it does get the job done. :D –  Jun 25 '14 at 11:58
  • 1
    If you override the consts, does that only affect the model that you set them in, or does it override the model it extends as well, therefore affecting all models? – user1894292 Jun 26 '14 at 08:15
  • 4
    Of course only the model you set them in AND all that inherit from it, but not parent class - `Eloquent\Model`. – Jarek Tkaczyk Jun 26 '14 at 08:27
  • on every GET output it gives json object of timestamp instead of just date – cosmoloc Nov 09 '17 at 07:24
  • @zeetit irrelevant, change what you're returning from your HTTP method. – Jarek Tkaczyk Nov 15 '17 at 13:48
30

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

  • Check my answer as this is going to lead to unexpected behaviour. – Jarek Tkaczyk Jun 25 '14 at 10:13
  • I've looked into the source again and it's as what deczo said, the code that I provide will result in error when it comes to other methods that use the static constants. I've updated the code example to reflect deczo's suggestion. –  Jun 25 '14 at 10:49
  • And for laravel9: https://github.com/illuminate/database/blob/9.x/Eloquent/Model.php#L189 – Blues Clues Apr 07 '22 at 00:27
  • Beware! class YourModel extends Model, not Eloquent. – Marcello Marino Sep 07 '22 at 09:38
3

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';
}
Shamseer Ahammed
  • 1,787
  • 2
  • 20
  • 25
-1

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.

alexrussell
  • 13,856
  • 5
  • 38
  • 49