雄辩的updateOrCreate总是创建

我正在尝试使用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());

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


狐的传说
浏览 160回答 2
2回答

三国纷争

您想尝试以下一种方法:    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        ]    );
打开App,查看更多内容
随时随地看视频慕课网APP