我正在尝试为用户添加新的注册详细信息,即用户的LASTNAME。但我无法将其插入我的数据库。这是出现的错误
ERROR:
Connection.php第761行中的QueryException: SQLSTATE [HY000]:常规错误:1364字段' f_lastname'没有默认值(SQL:插入
users
(name
,password
,updated_at
,created_at
)价值观(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。感谢你的帮助!
答案 0 :(得分:1)
错误是自我解释的。
它声明列 f_lastname 不为空,并且没有为其分配默认值。因此,在您的查询中,您必须包含具有某个值的列,或者更改表结构并为其指定一些默认值或使其为null null。
前:
将列更改为允许null:
ALTER TABLE `user` CHANGE `f_lastname` `f_lastname` TEXT NULL
或者,将其设为默认值为空字符串:
ALTER TABLE `user` CHANGE `f_lastname` `f_lastname` TEXT NOT NULL DEFAULT ''