【发布时间】:2019-08-01 05:11:56
【问题描述】:
我正在尝试使用 laravel 自己的身份验证功能在 laravel 中创建用户。 我已启用它并希望将一些我自己的值添加到用户表中的字段。
现在我收到此错误,但不知道如何修复它。有任何想法吗? :
数组到字符串的转换(SQL:插入users(name,email,password,activation_token,is_active,is_expert,is_flag,profile_img,@987654 @, updated_at, created_at) 值 (Henrik Tobiassen, tarzan@hotmail.com, $2y$10$.xmKCnSdC8vogY47mbwRVOVehXJIedLMJ/gpfNgluPR9QpOtgyJ1m, 4633, 0, 0, 0, 0, uploads/profile_pics/default.jpg, 2 03-10 21:12:29, 2019-03-10 21:12:29)
应用程序/user.php
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'password', 'remember_token', 'activation_token', 'is_active', 'is_expert', 'is_flag', 'is_subscriber', 'profile_img',
];
注册控制器
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'activation_token' => [rand(1000, 9999)],
'is_active' => '0',
'is_expert' => '0',
'is_flag' => '0',
'is_subscriber' => '0',
'profile_img' => 'uploads/profile_pics/default.jpg',
]);
}
【问题讨论】:
-
您也可以使用
protected $attributes = [...]或 Accessors & Mutators 来设置默认值,而不是编辑 RegisterController - 请参阅 this answer 中的更多信息.这样,您的逻辑将适用于在应用程序中创建的任何用户,而不仅仅是注册用户...
标签: php laravel laravel-authentication