Laravel 5.3: 无法为用户注册添加额外的用户信息

Laravel 5.3: Cannot add Additional user info for User registration

我正在尝试为用户添加新的注册详细信息,即用户的姓氏。但是我无法将它插入到我的数据库中。这是出现的错误

错误:

QueryException in Connection.php line 761: SQLSTATE[HY000]: General error: 1364 Field 'f_lastname' doesn't have a default value (SQL: insert into users (name, email, password, updated_at, created_at) values (chu, chu@yahoo.com, 111, 2016-10-12 06:55:33, 2016-10-12 06:55:33))

这是我的数据库迁移文件

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

user.php$可填写代码

class User extends Authenticatable
{
use Notifiable;

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

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

RegistrationController.php验证器部分

  protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|max:255|unique:users',
        'f_lastname' => 'required|max:255|unique:users',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);
}

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

我看到的大多数教程都是针对 laravel 5.2 的。我刚开始使用 laravel 5.3。感谢你们的帮助!

错误是不言自明的。

它声明列 f_lastname 不为空,并且没有为其分配默认值。因此,在您的查询中,您必须将该列包含一些值或更改 table 结构并为其分配一些默认值或使其为空接受列。

例如:

更改列以允许空值:

ALTER TABLE `user` CHANGE `f_lastname` `f_lastname` TEXT NULL

或者,给它一个默认值作为空字符串:

ALTER TABLE `user` CHANGE `f_lastname` `f_lastname` TEXT NOT NULL DEFAULT ''