首页 > 解决方案 > laravel updateOrCreate 查询空数据

问题描述

尝试使用雄辩的方法 updateOrCreate() 没有成功,下面是我的代码

$myArray = ['nama_toko' => 'test', 'user_id' => 3];
Store::updateOrCreate(['user_id' => 2], $myArray);

它显示如下错误:

null value in column "user_id" violates not-null constraint DETAIL: Failing row contains 
(22, test, null, 2020-09-20 01:14:30, 2020-09-20 01:14:30). 
(SQL: insert into "stores" ("nama_toko", "updated_at", "created_at") 
values (test, 2020-09-20 01:14:30, 2020-09-20 01:14:30) returning "id") 

为什么它不能正确读取我的数组(user_id)。请帮助和感谢。

这是我的商店表:

public function up()
    {
        Schema::create('stores', function (Blueprint $table) {
            $table->id();
            $table->string('nama_toko');
            $table->bigInteger('user_id');
            $table->timestamps();
        });
    }

标签: laravellaravel-7

解决方案


确保将 user_id 作为可填充属性添加到您的模型中。


推荐阅读