【问题标题】:Create user in Laravel with custom fields在 Laravel 中使用自定义字段创建用户
【发布时间】:2019-08-01 05:11:56
【问题描述】:

我正在尝试使用 laravel 自己的身份验证功能在 laravel 中创建用户。 我已启用它并希望将一些我自己的值添加到用户表中的字段。

现在我收到此错误,但不知道如何修复它。有任何想法吗? :

数组到字符串的转换(SQL:插入usersnameemailpasswordactivation_tokenis_activeis_expertis_flagprofile_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


【解决方案1】:

您似乎正在尝试发送一个数组

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

而不是字符串/数字

修复->

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

【讨论】:

    【解决方案2】:

    可能的原因是您将随机变量设为数组。删除它周围的方括号。这现在将返回一个字符串而不是一个数组。

    查看底部的示例进行演示。

    更正的代码:

        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',
        ]);
    }
    

    示例: http://sandbox.onlinephpfunctions.com/code/1977ba4dbe2f22870e0e81a628749c39c367747a

    【讨论】:

    • 就是这样。很简单。谢谢各位
    • @htobiassen 完美!通常是小事!
    【解决方案3】:

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

    所以不是

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 2020-02-18
      • 2014-04-30
      • 1970-01-01
      • 2015-02-16
      • 2019-09-26
      相关资源
      最近更新 更多