为了账号安全,请及时绑定邮箱和手机立即绑定

雄辩的updateOrCreate总是创建

雄辩的updateOrCreate总是创建

PHP
狐的传说 2021-04-26 16:29:16
我正在尝试使用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

        ]

    );


查看完整回答
反对 回复 2021-05-14
  • 2 回答
  • 0 关注
  • 159 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信