简体   繁体   English

Laravel 5.3:无法为用户注册添加其他用户信息

[英]Laravel 5.3: Cannot add Additional user info for User registration

Im trying to add a new registration detail for the user Which is the user's LASTNAME. 我正在尝试为该用户的姓氏添加新的注册详细信息。 But Im having trouble in inserting it to my database. 但是我在将它插入我的数据库时遇到了麻烦。 This is the error that comes up 这是出现的错误

ERROR: 错误:

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)) Connection.php 761行中的QueryException:SQLSTATE [HY000]:常规错误:1364字段“ f_lastname”没有默认值(SQL:插入usersnameemailpasswordupdated_atcreated_at )中的值(chu,chu @ yahoo.com,111,2016-10-12 06:55:33,2016-10-12 06:55:33)

This is my database migration file 这是我的数据库迁移文件

  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();
    });
}

The user.php $fillable code user.php $ fillable代码

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',
];
}

and the RegistrationController.php validator section 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']),
    ]);
}

Most of the tutorials I saw was for laravel 5.2. 我看到的大多数教程都是针对laravel 5.2的。 Im just new in using laravel 5.3. 我刚刚使用laravel 5.3。 Appreciate your help guys! 感谢您的帮助!

Error is self explanatory. 错误是不言自明的。

It states the column f_lastname is not null and there is no default value assigned to it. 它指出f_lastname列不为null,并且没有分配默认值。 So in your query you have to include that column with some value or change the table structure and assign some default value to it or make it null accepted column. 因此,在查询中,您必须为该列添加一些值,或者更改表结构并为其分配一些默认值,或者使其成为null接受的列。

Ex : 例如:

Change the column to allow null: 将列更改为允许为空:

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

or, give it a default value as empty string: 或者,将其默认值设置为空字符串:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM