php 如何在 Laravel 中只使用 created_at

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29886497/
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-26 00:38:14  来源:igfitidea点击:

How to only use created_at in Laravel

phplaravel

提问by refear99

I want only use created_at , how to do it?

我只想使用 created_at ,怎么做?

I know:

我知道:

This can custom timestamps name

这可以自定义时间戳名称

const CREATED_AT = 'created';
const UPDATED_AT = 'updated';

This can disable timestamps

这可以禁用时间戳

public $timestamps = false;

回答by Joseph Silber

Eloquent does not provide such functionality out of the box, but you can create it on your own using the creatingevent callback:

Eloquent 没有提供这种开箱即用的功能,但您可以使用creating事件回调自己创建它:

class User extends Eloquent {

    public $timestamps = false;

    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->created_at = $model->freshTimestamp();
        });
    }

}

回答by Rodolfo Jorge Nemer Nogueira

Use on the top:

在顶部使用:

const UPDATED_AT = null;

And for 'created_at' field, you can use:

对于“created_at”字段,您可以使用:

const CREATED_AT = null;

UPDATE FOR LARAVEL 5.5.0 - 5.5.5

更新 Laravel 5.5.0 - 5.5.5

This was broken in Laravel 5.5.0(and fixed again in 5.5.5).

在 Laravel 5.5.0 中被打破(并在 5.5.5 中再次修复)。

If you are using 5.5.x, make sure you are on the newest version.

如果您使用的是 5.5.x,请确保您使用的是最新版本。

If for some reason you cannot be on the newest version, here is a workaround.

如果由于某种原因您无法使用最新版本,这里有一个解决方法。

Set the public $timestamps to false:

将公共 $timestamps 设置为 false:

public $timestamps = false;

And when necessary:

必要时:

public function setCreatedAtAttribute($value) { 
    $this->attributes['created_at'] = \Carbon\Carbon::now(); 
}

OR

或者

public function setUpdatedAtAttribute($value) { 
    $this->attributes['updated_at'] = \Carbon\Carbon::now(); 
}

When the two fields "created_at" and "updated_at" are required, you do not have to do anything, of course ;)

当需要“created_at”和“updated_at”这两个字段时,您当然不必做任何事情;)

回答by Abrar Jahin

I have a better answer from herefor Laravel 5.2 or above.

我从这里Laravel 5.2 或更高版本有更好的答案。

U can use this in model-

你可以在模型中使用它-

class User extends Model
{
    public $timestamps = false; // disable all behavior
    public $timestamps = true; // enable all behavior
    public $timestamps = [ "created_at" ]; // enable only to created_at
    public $timestamps = [ "updated_at" ]; // enable only to updated_at
    public $timestamps = [ "created_at", "updated_at" ]; // same as true, enable all behavior
}

So, for your question, the answer is -

所以,对于你的问题,答案是——

public $timestamps = [ "created_at" ]; // enable only to created_at

回答by Dan

My solution:

我的解决方案:

class CreatePostsTable extends Migration {

   public function up() {

      Schema::create('posts', function(Blueprint $table){

         $table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
   });
}

This works for me

这对我有用

回答by sathish R

I solved by adding default value CURRENT_TIMESTAMPin my database for the column created_at. And In my model I use this below code

我通过CURRENT_TIMESTAMP在我的数据库中为 column添加默认值来解决created_at。在我的模型中,我使用下面的代码

public $timestamps = false;
protected $dates = ['created_at'];

I hope this method works in all versions of laravel.

我希望这种方法适用于所有版本的 Laravel。

回答by Yevgeniy Afanasyev

On your modelset

在你的model集合

const UPDATED_AT = null;

const UPDATED_AT = null;

or

或者

const CREATED_AT = null;

const CREATED_AT = null;

it stops Laravel trying to update the updated_at/created_at field

它阻止 Laravel 尝试更新 updated_at/created_at 字段

It works for Laravel 5.8

它适用于 Laravel 5.8

And it is better than overwrite: setUpdatedAtin your model

它比覆盖更好:setUpdatedAt在您的model

public function setUpdatedAt($value) : self 
{
    // Do nothing.
    return $this;
}

because it is shorter and because the check for if (! is_null(static::UPDATED_AT)is happening in source code earlier than triggering setUpdatedAt

因为它更短,并且因为if (! is_null(static::UPDATED_AT)在源代码中检查发生在触发之前 setUpdatedAt

回答by absolux

To disable only updated_at, you can override Model::setUpdatedAt() method, like following :

要仅禁用 updated_at,您可以覆盖 Model::setUpdatedAt() 方法,如下所示:

public function setUpdatedAt($value) {
    // Do nothing.
}

Of course, if you wanted to do this for the created_at column, it's just as easy. And that's work for Laravel 5.1

当然,如果您想对 created_at 列执行此操作,也同样简单。这适用于 Laravel 5.1

回答by m0rPh1n3

In version 5.3 I just did public $timestamps = false;and then added protected $fillable = ['created_at']. Note: You can add anything you want, just make sure it matches the one in your table.`

在 5.3 版中,我刚刚做了public $timestamps = false;,然后添加了受保护的 $fillable = ['created_at']。注意:您可以添加任何您想要的内容,只需确保它与您表格中的内容匹配即可。`

回答by pablorsk

Since Laravel 5.*

自 Laravel 5.*

Model:

模型:

//  Disable updated_at (only created_at)
class Book extends Model
{
     const UPDATED_AT = null;

     /* ... */
}

Migration:

移民:

Schema::create('books', function (Blueprint $table): void {
    /* ... */
    $table->timestampTz('created_at')->nullable();
});

回答by user3213674

Use on the top the class:

在类的顶部使用:

const UPDATED_AT = null;

const UPDATED_AT = null;

or

或者

const CREATED_AT = null;

const CREATED_AT = null;