Why does Laravel 5 auto update my date field? Why does Laravel 5 auto update my date field? laravel laravel

Why does Laravel 5 auto update my date field?


It looks like what is happening is that MySQL (or whichever database you are using) is updating the date, not Laravel.

You need to change:

$table->timestamp('published_at');

to:

$table->dateTime('published_at');

Then do a php artisan migrate:refresh to rollback and recreate your tables.


This Post is old but for anyone who got this problem here is the simplest solution. In your migration the timestamp field just has to be nullable. Than there will no auto update trigger on this field.

$table->timestamp('published_at')->nullable();

No need for using $table->dateTime() instead of $table->timestamp().


This is not a problem of Laravel, but a "feature" of MySQL.

According to the reference manual MySQL 5.6

TIMESTAMP and DATETIME columns have no automatic properties unless they are specified explicitly, with this exception: If the explicit_defaults_for_timestamp system variable is disabled, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly.

The official site removed reference manual for older version, but when I google for the problem, from the sources that also mention about the problem, it seems that the feature was there for older version of MySQL too.

Therefore, instead of changing $table->timestamp('published_at'); to $table->dateTime('published_at');, you may also try to solve the problem by

  1. giving a default value to the timestamp. e.g. $table->timestamp('published_at')->default(DB::raw('CURRENT_TIMESTAMP')); or $table->timestamp('published_at')->nullable() (where the default value is null)
  2. moving the line later then $table->timestamps(); so that pulibhsed_at is not the firs timestamp. The default created_at and updated_at is nullable (i.e. having default value null) an will not trigger the "feature"