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, [email protected], $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',
        ]);
    }
1
  • You could have also used protected $attributes = [...] or Accessors & Mutators to set default the values instead of editing RegisterController - see more in this answer. This way, your logic would apply to any user that is created in the application, and not just register user... Commented Mar 10, 2019 at 21:56

3 Answers 3

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),
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

That was it. So simple. Thank you guys
@htobiassen Perfect! Often it's the small things!
1

seems you are trying to send an array

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

instead of string/number

Fix ->

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.