首页 > 解决方案 > 为什么时间戳 col 为空

问题描述

我将时间戳 cols 添加到数据库表中,

public function up()
    {
        Schema::table('allestates', function (Blueprint $table) {
            $table->timestamps();
        });
    }

并且还真实地融入了表格的模型。

    public $timestamps = true;

    protected $table = "allestates";

但是当我添加新的数据时间戳时,cols 仍然是 Null?知道为什么会这样吗? 在此处输入图像描述

标签: phplaravellaravel-5

解决方案


Timestamps 为 null 是因为您在插入数据时可能没有使用 laravel eloquent。

$model= new model;

$model->column_one= 'John';

$model->save();

或者你可以使用,

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

推荐阅读