How to only use created_at in Laravel

后端 未结 17 2057
自闭症患者
自闭症患者 2020-12-07 18:20

I want only use created_at , how to do it?

I know:

This can custom timestamps name

const CREATED_AT = \'created\';
const UPDATED_AT = \'updat         


        
相关标签:
17条回答
  • 2020-12-07 19:08

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

    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

    0 讨论(0)
  • 2020-12-07 19:13

    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();
            });
        }
    
    }
    
    0 讨论(0)
  • 2020-12-07 19:14

    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 ;)

    0 讨论(0)
  • 2020-12-07 19:15

    Since 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();
    });
    
    0 讨论(0)
  • 2020-12-07 19:16

    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
    

    Re-

    public $timestamps = [ "created_at" ]; 
    

    Not working in Laravel 6+

    0 讨论(0)
提交回复
热议问题