gpt4 book ai didi

php - Eloquent updateOrCreate 总是创建

转载 作者:行者123 更新时间:2023-12-01 00:08:37 25 4
gpt4 key购买 nike

我正在尝试使用 updateOrCreate 来简化我的代码,但函数总是创建一个新行,从不更新。

我的迁移:

        Schema::create('logs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('project_id')->unsigned();
$table->datetime('is_fixed')->nullable();
$table->datetime('snooze_until')->nullable();
$table->integer('snooze_while')->nullable();
$table->string('title', 100);
$table->string('level', 100);
$table->string('description');
$table->string('stage',100);
$table->json('details')->nullable();
$table->timestamps();

$table->foreign('project_id')->references('id')->on('projects');
});

我的 $fillables

protected $fillable = [
'project_id',
'is_fixed',
'snooze_until',
'snooze_while',
'title',
'level',
'description',
'stage',
'details'
];

我的测试

    $log = new Log();

$log->fill([
'title' => 'Vicente\\Sally\\Schiller',
'level' => 'ERROR',
'description' => 'Laboriosam et architecto voluptatem.',
'stage' => 'production@wender-fedora',
'details' => '{"app":"MyApp"}',
'project_id' => 5
]);

Log::updateOrCreate($log->toArray());

我有一些可为空的字段,但我认为这不是问题。

最佳答案

updateOrCreate 方法接受两个数组参数:

  • 第一个是字段数组 => 将用作检查该行是否存在的条件的值。
  • 第二个是字段数组 => 要更新的值(如果记录与第一个参数的条件匹配)或要添加的值(如果找不到匹配项,则与第一个参数合并) .

所以我猜Jesus answer可能适合您。

关于php - Eloquent updateOrCreate 总是创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55839663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com