Laravel、MariaDB 时间戳列没有默认和 onupdate 触发器

Omi*_*mid -1 php migration blueprint laravel eloquent

我有以下迁移:

Schema::create('auctions', function (Blueprint $table) {
    $table->id();
    $table->timestamp('expired_at');
    $table->timestamp('sold_at')->nullable()->default(null);
    // other fields
});
Run Code Online (Sandbox Code Playgroud)

它创建了一个具有以下结构的表: 在此输入图像描述

每次我尝试sold_at仅更新字段时,它expire_at也会更改字段,这非常烦人并且违反我的项目逻辑!我应该如何修复我的迁移以防止这种情况发生?

我的更新记录代码:

Auction::query()->where('id',1)->update([
    'sold_at' => now()
]);
Run Code Online (Sandbox Code Playgroud)

ker*_*olz 5

将您的列更改为datetime列:

Schema::create('auctions', function (Blueprint $table) {
    $table->id();
    $table->dateTime('expired_at');
    $table->timestamp('sold_at')->nullable()->default(null);
    // other fields
});
Run Code Online (Sandbox Code Playgroud)

https://laravel.com/docs/9.x/migrations#column-method-dateTime