Laravel:在数据库连接上设置时间戳 laravel mysql



我想了解如何通过 laravel 设置与 mysql 数据库连接的每个连接的时间戳 是否有任何配置有助于实现这一目标。

每当

模型更新时,Eloquent 都会自动更新 updated_at 属性。只需将时间戳添加到迁移中,如下所示:

$table->timestamps();

然后created_atupdated_at字段将添加到您的表中,Eloquent 将自动使用它们。

来自 laravel 文档的完整示例:

<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps(); // <<< Adds created_at and updated_at
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

最新更新