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

时间:2016-02-22 08:10:41

标签: laravel-5

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

1 个答案:

答案 0 :(得分:0)

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

$table->timestamps();
然后会将

created_atupdated_at字段添加到您的表中,并且eloquent将自动使用它们。

Full example from the laravel docs

<?php

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

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