Home > OS >  Add updated_at column with default value same as created_at
Add updated_at column with default value same as created_at

Time:09-16

I have column 'created_at' in my table, now I want to add 'updated_at' with default value same as 'created_at'. Im using laravel migration.

        Schema::table($this->tableName(), function (Blueprint $table) {
            $table->timestamp('updated_at');
        });

CodePudding user response:

This would most likely be a one-off, since by default using Eloquent models, it will update both created_at and updated_at when creating a model. So after the code above, add in

DB::statement("UPDATE ".$this->tableName()." SET updated_at = created_at");

Make sure that you remove any default timestamp settings from your model, and Laravel will take it from there.

  • Related