how would you go about adding custom fields to the laravel auth registration process. I would like to check which country they're from. I was going to add it to register controller but I'm not sure how to pass the array to the view. and when i do add it to the controller it throws up and error. Any advice would be wonderful.
1 Answer
First, you will need to add a column on the users table to store this information. You can do this with a migration. Put something like this into the up() function.
Schema::table('users', function (Blueprint $table) {
$table->string('country');
});
Next you will need to add it to the $fillable array in User.php like this, so that the value provided will be saved.
protected $fillable = [
'name', 'email', 'password', 'country'
];
Then you should add it to the create function in RegisterController like this. This will connect the value that the user provided with the new User object that will be saved.
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'country' => $data['country'],
]);
}
You will also need to add it to register.blade.php so the user can provide the information.
<label for="countryInput" class="col-md-4 control-label">Country</label>
<input id="countryInput" type="text" class="form-control" name="country">
Finally, if you want to validate this field you should add it in the validator function (in the RegisterController).