encrypted21 encrypted21 - 3 months ago 11
MySQL Question

How to write a text into one database column for every new user during registration in Laravel 5.2?

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?

Answer

Are 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'];
Comments