laravel created_at 和 updated_at 是反的吗?

Is laravel created_at and updated_at inverse?

Laravel 框架版本 5.2.5

当我更新记录时,update_at 没有改变,但 created_at 变成了当前时间戳。

这是正确的吗?

MariaDB [moon]> show columns from users;
+----------------+------------------+------+-----+---------------------+-----------------------------+
| Field          | Type             | Null | Key | Default             | Extra                       |
+----------------+------------------+------+-----+---------------------+-----------------------------+
| id             | int(10) unsigned | NO   | PRI | NULL                | auto_increment              |
| name           | varchar(255)     | NO   |     | NULL                |                             |
| email          | varchar(255)     | NO   | UNI | NULL                |                             |
| password       | varchar(60)      | NO   |     | NULL                |                             |
| remember_token | varchar(100)     | YES  |     | NULL                |                             |
| created_at     | timestamp        | NO   |     | CURRENT_TIMESTAMP   | on update CURRENT_TIMESTAMP |
| updated_at     | timestamp        | NO   |     | 0000-00-00 00:00:00 |                             |
+----------------+------------------+------+-----+---------------------+-----------------------------+
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

查看您的数据库结构,问题是 on update CURRENT_TIMESTAMP 用于 created_at 列。明明不应该是这样的。查看迁移,此迁移不太可能自动设置,因此可能已在 MySQL 中手动设置,因此您应该删除 created_at 列的 on update CURRENT_TIMESTAMP

当您使用 Eloquent 更新模型时,updated_at 列应该可以毫无问题地更新(也在当前数据库模式中)- 如果不是,您可能不会使用 Eloquent ,所以它不会自动更新。如果您不确定,请显示您的代码如何更新此记录

最近很多人遇到了这个问题,你可以在这里阅读 Github 上的讨论:https://github.com/laravel/framework/issues/11518

MySQL 5.7 no longer allows 0000-00-00 as a valid timestamp with strict mode turned on (which it is by default). So either use ->nullableTimestamps() or ->timestamp()->useCurrent().

您可以通过更改以下内容来解决此问题:

$table->timestamps();

选择以下任一选项:

// Option 1:
$table->nullableTimestamps();

// Option 2:
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();

此外,在此 MySQL 页面上:https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html

Alternatively, if explicit_defaults_for_timestamp is disabled (the default), do either of the following:

Define the column with a DEFAULT clause that specifies a constant default value.

Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.

无论哪种情况,上述解决方案都应该可以解决您的问题。

大家好,我叫 arash laghaei,来自伊朗 我解决了这个问题 请这样做: vendor/laravel/framework/srs/illuminate/database/schema/blueprint.php line:792

 public function timestamps()
{
    $this->timestamp('updated_at');

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

}

请照我写的做。 并解决了这个问题。