drhib62644 2016-10-12 07:04
浏览 61
已采纳

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

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))

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

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

  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. Im just new in using laravel 5.3. Appreciate your help guys!

  • 写回答

1条回答 默认 最新

  • douzi7890 2016-10-12 07:11
    关注

    Error is self explanatory.

    It states the column f_lastname is not null and there is no default value assigned to it. 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.

    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 ''
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改