Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
451 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...