Я пытаюсь использовать 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());
У меня есть поля, допускающие значение NULL, но я считаю, что это не проблема.






Вы хотите попробовать это:
Log::updateOrCreate(
[
'title' => 'Vicente\\Sally\\Schiller',
'project_id' => 5
],
[
'title' => 'Vicente\\Sally\\Schiller',
'level' => 'ERROR',
'description' => 'Laboriosam et architecto voluptatem.',
'stage' => 'production@wender-fedora',
'details' => '{"app":"MyApp"}',
'project_id' => 5
]
);
Метод updateOrCreate принимает два параметра массива:
Так что я думаю, что Иисус ответ может быть правильным для вас.