php 完整性约束冲突:19非空值

r1zk6ea1  于 2023-03-16  发布在  PHP
关注(0)|答案(2)|浏览(163)

说明\数据库\查询异常
数据库状态[23000]:完整性约束冲突:19 NOT NULL约束失败:用户。密码(SQL:插入到“用户”(“姓名”、“电子邮件”、“用户名”、“更新时间”、“创建时间”)值中(MADHUP KUMAR,chandrashivam99@gmail.com,Vansh123,2020年6月12日09:38:46,2020年6月12日09:38:46))
http://127.0.0.1:8000/register

用户.php模型

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email','username','password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
         'password','remember_token',
    ];

创建用户表.php迁移

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->string('username')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

注册控制器.php

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'username' => ['required', 'string', 'min:5','unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],

    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'username' => $data['username'],
        'password' > Hash::make($data['password'])
    ]);
}
but5z9lq

but5z9lq1#

您忘记了数组中的=。请在create方法中更改

'password' > Hash::make($data['password'])

'password' => Hash::make($data['password'])
wgeznvg7

wgeznvg72#

下面是解决这个问题的方法:后藤app〉Models〉User.php并将username添加到可填充函数中。

protected $fillable = [
    'name',
    'email',
    'password',
    'username',
];

相关问题