Disable Laravel's Eloquent timestamps

后端 未结 10 1447
余生分开走
余生分开走 2020-12-04 08:54

I\'m in the process of converting one of our web applications from CodeIgniter to Laravel. However at this moment we don\'t want to add the updated_at / c

相关标签:
10条回答
  • 2020-12-04 09:33

    In case you want to remove timestamps from existing model, as mentioned before, place this in your Model:

    public $timestamps = false;
    

    Also create a migration with following code in the up() method and run it:

    Schema::table('your_model_table', function (Blueprint $table) {
        $table->dropTimestamps();
    });
    

    You can use $table->timestamps() in your down() method to allow rolling back.

    0 讨论(0)
  • 2020-12-04 09:36

    Eloquent Model:

    class User extends Model    
    {      
        protected $table = 'users';
    
        public $timestamps = false;
    }
    

    Or Simply try this

    $users = new Users();
    $users->timestamps = false;
    $users->name = 'John Doe';
    $users->email = 'johndoe@example.com';
    $users->save();
    
    0 讨论(0)
  • 2020-12-04 09:40

    Override the functions setUpdatedAt() and getUpdatedAtColumn() in your model

    public function setUpdatedAt($value)
    {
       //Do-nothing
    }
    
    public function getUpdatedAtColumn()
    {
        //Do-nothing
    }
    
    0 讨论(0)
  • 2020-12-04 09:46

    If you only need to only to disable updating updated_at just add this method to your model.

    public function setUpdatedAtAttribute($value)
    {
        // to Disable updated_at
    }
    

    This will override the parent setUpdatedAtAttribute() method. created_at will work as usual. Same way you can write a method to disable updating created_at only.

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