首页 > 解决方案 > 在 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',
        ]);
    }

标签: phplaravellaravel-authentication

解决方案


这可能是因为您将数组而不是数字或字符串发送到 activation_token 字段。

所以而不是

'activation_token' => [rand(1000,9999)],

'activation_token' => rand(1000,9999),

推荐阅读