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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改