【发布时间】:2019-01-12 08:41:24
【问题描述】:
我正在使用 updateOrCreate 函数来创建新行或根据给定值更新存在。但是,当我尝试更新定义为时间戳的值(不是默认的“created_at”或“updated_at”)时,它不会更新该值。
仅当我使用 updateOrCreate 函数并且仅当要更新的唯一字段是时间戳字段时才会出现此问题。 (在我的情况下,'last_scan')。 例如,当我将“状态”更改为 0 时,数据库中更新的所有值都包含“last_scan”字段。
我想验证每次我运行这个函数时,无论是否有数据要更新,last_scan都会更新到函数运行的时间。
$categoryEntity = CategoryEntity::updateOrCreate(
[
'store_id' => $this->store_id,
'remote_id' => $collection->id
],
[
'remote_parent_id' => 0,
'name' => $collection->title,
'status' => 1,
'last_scan' => Carbon::now()
]
);
我尝试使用以下常规更新方法替换更新并创建,并且效果很好:
$categoryEntity->last_scan = Carbon::now();
$categoryEntity->save();
我的迁移文件如下:
Schema::create('categories_entities', function (Blueprint $table) {
$table->increments('id');
$table->integer('store_id');
$table->integer('remote_id');
$table->integer('remote_parent_id');
$table->string('name');
$table->integer('status');
$table->timestamp('last_scan');
$table->timestamps();
});
【问题讨论】:
-
你用的是哪个版本的 laravel?
-
我的 Laravel 是 v5.7
标签: laravel laravel-5 eloquent