Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change name of Laravel's created_at and updated_at

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.

like image 855
user1894292 Avatar asked Jun 25 '14 09:06

user1894292


1 Answers

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.

like image 69
Jarek Tkaczyk Avatar answered Sep 30 '22 05:09

Jarek Tkaczyk