我正在嘗試使用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'); });我的$ fillablesprotected $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());我有一些可為空的字段,但我認為這不是問題。
2 回答

三國紛爭
TA貢獻1804條經驗 獲得超7個贊
您想嘗試以下一種方法:
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
]
);
- 2 回答
- 0 關注
- 180 瀏覽
添加回答
舉報
0/150
提交
取消