首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel 5.5.* SQLSTATE[23000]:完整性约束冲突: 19非空约束失败

Laravel 5.5.* SQLSTATE[23000]:完整性约束冲突: 19非空约束失败
EN

Stack Overflow用户
提问于 2019-09-29 17:02:26
回答 1查看 179关注 0票数 0

我正在尝试使用Laravel提供的RegistrationController向我的数据库添加一个新用户。每次我尝试注册一个新用户时,它都会给我一个错误,告诉我not null约束已经失败,即使我没有提交任何null字段。起初,我认为这是一个地址字段的问题,直到我在迁移中将其设置为可空以避免错误,并且对下一个字段的not null约束也失败了。我已经仔细检查了我的所有迁移文件,甚至将我试图传递到数据库中的数据转储到数据库中,以仔细检查nothing is null。错误消息告诉我,它甚至没有尝试将我提供的信息传递给数据库,但我不知道如何解决这个问题。

我的迁移:

代码语言:javascript
运行
复制
    Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->string('address');
            $table->string('user_type');
            $table->boolean('approved')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });

我在注册控制器中的创建函数:

代码语言:javascript
运行
复制
    protected function create(array $data)
    {
        return User::create([
            'address' => $data['address'],
            'name' => $data['name'],
            'email' => $data['email'],
            'user_type' => $data['user_type'],
            'password' => Hash::make($data['password'])
        ]);
    }

我的数据转储的结果:

代码语言:javascript
运行
复制
       array:7 [▼
  "_token" => "d5qCwoIqyfiF1q5FyAIhi3gHEm5CA7OHpDZVRKSI"
  "name" => "test"
  "email" => "asasdfasd@sdfgsdfgg.com"
  "address" => "1637 test street"
  "user_type" => "Customer"
  "password" => "12345678"
  "password_confirmation" => "12345678"
]

我得到的错误是:

代码语言:javascript
运行
复制
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: 
users.address (SQL: insert into "users" ("name", "email", "password", "updated_at", "created_at") 
values (test, asasdfasd@sdfgsdfgg.com, $2y$10$q1Kc0A80cd0ARJbvTtpiee59fJ8g4orilFWjNV5igihPDWQeoYhjG, 2019-09-29 08:57:58, 2019-09-29 08:57:58))

任何人能为我提供的任何帮助都将不胜感激。如果你需要更多我的代码片段,请让我知道。

EN

回答 1

Stack Overflow用户

发布于 2019-09-29 17:20:35

在您的模型中,您应该添加fillable属性以允许批量分配。

代码语言:javascript
运行
复制
class User extends Model
{
    protected $fillable = [
        'address', 'name', 'email', 'password', 'user_type'
    ];
}

或者只是使用

代码语言:javascript
运行
复制
class User extends Model
{
    protected $guarded = [];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58153786

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档