3

I'm using Laravel 5.2 Auth feature and registration works fine.

Now I need to write a string in a special column in users table during registration. It's not a value from an HTML form input. It's a static text describing a user role (contributor). All users registering should receive a "contributor" value in user_role column. I tried to achieve this by adding this line to AuthController.php:

'user_role' => 'contributor',

The whole function adding values to database looks like this:

/**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'user_role' => 'contributor',
        ]);
    }

When I try to register a new account, everything except 'user_role' => 'contributor', gets added.
What's the proper way to write "contributor" value for every new user into the user_role column?
Is it not working because that function only has the $data array set?

encrypted21
  • 194
  • 11
  • Possible duplicate of [How to set a default attribute value for a Laravel / Eloquent model?](http://stackoverflow.com/questions/18747500/how-to-set-a-default-attribute-value-for-a-laravel-eloquent-model) – aynber Aug 25 '16 at 13:37
  • This issue is completely different from the one you provided. It's from another part of Laravel. – encrypted21 Aug 25 '16 at 17:42

1 Answers1

3

Did you add user_role to $fillable array on User model ?

If not you need to add it

Example

protected $fillable = ['name', 'email', 'password', 'user_role'];
Skysplit
  • 1,875
  • 12
  • 16
Dinh Phong
  • 596
  • 3
  • 12