How to only use created_at in Laravel

2020-02-07 16:54发布

I want only use created_at , how to do it?

I know:

This can custom timestamps name

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

This can disable timestamps

public $timestamps = false;

标签: php laravel
17条回答
Explosion°爆炸
2楼-- · 2020-02-07 16:59

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

class User extends Eloquent {

    public $timestamps = false;

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

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

}
查看更多
看我几分像从前
3楼-- · 2020-02-07 16:59

I used very simple hack ;)

class MyClass extends Model {

const CREATED_AT = 'creation_date';
const UPDATED_AT = 'creation_date';

}

I just pointed both to the same column :)

查看更多
Lonely孤独者°
4楼-- · 2020-02-07 17:01

Use on the top:

const UPDATED_AT = null;

And for 'created_at' field, you can use:

const CREATED_AT = null;

UPDATE FOR LARAVEL 5.5.0 - 5.5.5

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

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

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

Set the public $timestamps to 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 ;)

查看更多
We Are One
5楼-- · 2020-02-07 17:06

I have a better answer from here for Laravel 5.2 or above.

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
查看更多
6楼-- · 2020-02-07 17:07

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

查看更多
够拽才男人
7楼-- · 2020-02-07 17:07

On your model set

const UPDATED_AT = null;

or

const CREATED_AT = null;

it stops Laravel trying to update the updated_at/created_at field

It works for Laravel 5.8

And it is better than overwrite: setUpdatedAt in your 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

查看更多
登录 后发表回答