php 更改 Laravel 的 created_at 和 updated_at 的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24404474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:18:25  来源:igfitidea点击:

Change name of Laravel's created_at and updated_at

phpmysqllaravellaravel-4

提问by user1894292

Can I map Laravel's timestamps from:

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

created_atto post_dateand post_date_gmt?

created_atpost_datepost_date_gmt

updated_atto post_modifiedand post_modified_gmt?

updated_atpost_modifiedpost_modified_gmt



I'm migrating a Wordpress application to Laravel.

我正在将 Wordpress 应用程序迁移到 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.

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

The Posts table has post_date, post_date_gmt, post_modifiedand post_modified_gmt, but Laravel is expecting created_atand updated_at.

该帖子表有post_datepost_date_gmtpost_modifiedpost_modified_gmt,但Laravel期待created_atupdated_at

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.

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

回答by Jarek Tkaczyk

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 getCreatedAtColumnand getUpdatedAtColumnwill return post_dateand post_modifiedrespectively, but won't do any harm.

then 方法getCreatedAtColumngetUpdatedAtColumn将分别返回post_datepost_modified,但不会造成任何伤害。

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

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

回答by user1894292

If you look into the source of the Eloquentclass

如果您查看Eloquent课程的来源

https://github.com/illuminate/database/blob/4.2/Eloquent/Model.php#L223-L235

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 _gmtversion of those timestamps, you might want to look into events. Here is a good start

至于_gmt这些时间戳的版本,您可能需要查看events. 这是一个好的开始

http://driesvints.com/blog/using-laravel-4-model-events

http://driesvints.com/blog/using-laravel-4-model-events

回答by alexrussell

You could try using the attribute getters and getters:

您可以尝试使用属性 getter 和 getter:

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.

我不知道这是否行得通,但你可以试一试。当然可能是一个基础 - 你必须玩弄它。

回答by Shamseer Ahammed

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';
}