Add updated_at column with default value same as created_at

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x