1

I am trying to create a user in laravel using laravel's own auth function. I have enabled it and want to add some of my own values to fields in the users table.

Right now I get this error, but dont know how to fix it. Any ideas? :

Array to string conversion (SQL: insert into users (name, email, password, activation_token, is_active, is_expert, is_flag, is_subscriber, profile_img, updated_at, created_at) values (Henrik Tobiassen, tarzan@hotmail.com, $2y$10$.xmKCnSdC8vogY47mbwRVOVehXJIedLMJ/gpfNgluPR9QpOtgyJ1m, 4633, 0, 0, 0, 0, uploads/profile_pics/default.jpg, 2019-03-10 21:12:29, 2019-03-10 21:12:29)

App/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',
    ];

RegisterController

 /**
     * 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',
        ]);
    }
Tridev Shrestha
  • 447
  • 7
  • 21
htobiassen
  • 61
  • 1
  • 9
  • You could have also used `protected $attributes = [...]` or **Accessors & Mutators** to set default the values instead of editing **RegisterController** - see more in [this answer](https://stackoverflow.com/a/39912500/3226121). This way, your logic would apply to any user that is created in the application, and not just register user... – ljubadr Mar 10 '19 at 21:56

3 Answers3

2

This might be happening because you're sending an array instead of a number or string to activation_token field.

So instead of

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

do

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

Likely the reason is you have made your random variable an array. Remove the square brackets around it. This will now return a string rather than an array.

Check the example at the bottom for a demonstration.

Corrected code:

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

Example: http://sandbox.onlinephpfunctions.com/code/1977ba4dbe2f22870e0e81a628749c39c367747a

Tim Rowley
  • 420
  • 4
  • 15
1

seems you are trying to send an array

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

instead of string/number

Fix ->

'activation_token' => rand(1000,9999),
Titu
  • 61
  • 1
  • 11