Change name of Laravel#39;s created_at and updated_at(更改 Laravel 的 created_at 和 updated_at 的名称)
问题描述
我可以从以下位置映射 Laravel 的时间戳吗:
Can I map Laravel's timestamps from:
created_at 到 post_date 和 post_date_gmt?
updated_at 到 post_modified 和 post_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_date、post_date_gmt、post_modified 和 post_modified_gmt,但 Laravel 需要 created_at 和 updated_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';
then 方法 getCreatedAtColumn 和 getUpdatedAtColumn 将分别返回 post_date 和 post_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 的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:更改 Laravel 的 created_at 和 updated_at 的名称
基础教程推荐
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
