更改Laravel的created_at和Updated_at的名称 [英] Change name of Laravel's created_at and updated_at

查看:175
本文介绍了更改Laravel的created_at和Updated_at的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从以下位置映射Laravel的时间戳吗?

Can I map Laravel's timestamps from:

created_atpost_datepost_date_gmt?

updated_atpost_modifiedpost_modified_gmt?

我正在将Wordpress应用程序迁移到Laravel.

I'm migrating a Wordpress application to Laravel.

我正在使用Laravel访问Wordpress数据库的副本.数据库的实时版本仍在使用中,因此我不想更改架构.

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.

Posts表具有post_datepost_date_gmtpost_modifiedpost_modified_gmt,但是Laravel期望的是created_atupdated_at.

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

反正有没有要更改Laravel查找的列名?

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

我希望Laravel更新所有已经存在的列的时间戳.

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

推荐答案

不幸的是,被接受的答案可能会导致时间戳更新方面的问题.

The accepted answer may cause problems with updating timestamps unfortunately.

您最好在模型上覆盖const:

You'd better override consts on your model:

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

然后方法getCreatedAtColumngetUpdatedAtColumn将分别返回post_datepost_modified,但不会造成任何危害.

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

对于其他列,您需要使用类似@Oni建议的事件.

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

这篇关于更改Laravel的created_at和Updated_at的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆