brti's avatar
Level 3

[5.2][MySQL 5.6] created_at field being updated on model save.

In my database the created_at field is being updated whenever a model is saved. I'm currently using Laravel 5.2, and MySQL 5.6.

Anyone else having a similar issue?

0 likes
4 replies
jhoff's avatar

Laravel shouldn't be doing that on it's own.

Is there a chance your database schema for that table is set to update created_at to the current timestamp when the record is changed?

brti's avatar
Level 3

In my migration I used $this->timestamps(); to declare the created_at, and updated_at fields. When I look at the schema in MySQL it has the created_at field set to update to the current timestamp whenever the record is changed.

EDIT: I should note I'm on Laravel 5.2.10

brti's avatar
Level 3

Ok I found a temporary fix, which basically involved manually declaring the timestamp fields like so:

$this->timestamp('created_at')->nullable();
$this->timestamp('updated_at')->useCurrent();

The nullable() function has to be used on the created_at field or else it will default to using the current timestamp whenever a record is updated. Still no idea why this is happening.

Please or to participate in this conversation.