我正在尝试使用Laravel提供的RegistrationController向我的数据库添加一个新用户。每次我尝试注册一个新用户时,它都会给我一个错误,告诉我not null约束已经失败,即使我没有提交任何null字段。起初,我认为这是一个地址字段的问题,直到我在迁移中将其设置为可空以避免错误,并且对下一个字段的not null约束也失败了。我已经仔细检查了我的所有迁移文件,甚至将我试图传递到数据库中的数据转储到数据库中,以仔细检查nothing is null。错误消息告诉我,它甚至没有尝试将我提供的信息传递给数据库,但我不知道如何解决这个问题。
我的迁移:
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();
});我在注册控制器中的创建函数:
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'])
]);
}我的数据转储的结果:
array:7 [▼
"_token" => "d5qCwoIqyfiF1q5FyAIhi3gHEm5CA7OHpDZVRKSI"
"name" => "test"
"email" => "asasdfasd@sdfgsdfgg.com"
"address" => "1637 test street"
"user_type" => "Customer"
"password" => "12345678"
"password_confirmation" => "12345678"
]我得到的错误是:
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))任何人能为我提供的任何帮助都将不胜感激。如果你需要更多我的代码片段,请让我知道。
发布于 2019-09-29 17:20:35
在您的模型中,您应该添加fillable属性以允许批量分配。
class User extends Model
{
protected $fillable = [
'address', 'name', 'email', 'password', 'user_type'
];
}或者只是使用
class User extends Model
{
protected $guarded = [];
}https://stackoverflow.com/questions/58153786
复制相似问题