php - 在 Laravel 中使用自定义字段创建用户
问题描述
我正在尝试使用 laravel 自己的身份验证功能在 laravel 中创建一个用户。我已启用它并希望将一些我自己的值添加到用户表中的字段中。
现在我得到这个错误,但不知道如何解决它。有任何想法吗?:
数组到字符串的转换(SQL:插入users
( name
, email
, password
, activation_token
, is_active
, is_expert
, is_flag
, is_subscriber
, profile_img
, updated_at
, created_at
) 值 (Henrik Tobiassen, tarzan@hotmail.com, $2y$10$.xmKCnSdC8vogY47mbwRVOVehXJIedLMJ/gpfNgluPR9QpOtgyJ1m, 4633, 0, 0, 0, 0 , 上传/profile_pics/default.jpg, 2019-03-10 21:12:29, 2019-03-10 21:12:29)
应用程序/用户.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',
]);
}
解决方案
这可能是因为您将数组而不是数字或字符串发送到 activation_token 字段。
所以而不是
'activation_token' => [rand(1000,9999)],
做
'activation_token' => rand(1000,9999),
推荐阅读
- javascript - 我的表格标题分为两行文本,我不知道为什么
- node.js - 从文本文件到 JSON 对象的文本数据
- tcl - 如何在另一个proc中引用proc中的变量
- c - Clinet 端 read() 获取消息的随机尾随字符(使用套接字的 TCP 客户端-服务器)
- c# - 如何从 zkteco 设备获取用户照片?
- java - 这个java代码对这个问题是否正确?(没有数组)
- java - 在 CanFly 接口中,必须使用一个 CanFly 类型的参数声明 speed 方法
- javascript - 在 HTML 画布上绘制线性渐变路径
- reactjs - 使用 Nginx 部署 React 加载资源失败
- algorithm - 如何有效地解决这个涉及“模”运算的编码问题?