Laravel将表单数据添加到身份验证注册



您将如何将自定义字段添加到Laravel身份验证注册过程中。我想检查他们来自哪个国家。我打算将其添加到注册控制器,但我不确定如何将数组传递给视图。当我将其添加到控制器时,它会抛出并出错。任何建议都会很棒。

首先,您需要在 users 表上添加一列来存储此信息。您可以通过迁移来执行此操作。将类似这样的东西放入up()函数中。

Schema::table('users', function (Blueprint $table) {
$table->string('country');
});

接下来,您需要像这样将其添加到User.php$fillable数组中,以便保存提供的值。

protected $fillable = [
'name', 'email', 'password', 'country'
];

然后你应该像这样RegisterController将其添加到创建函数中。这将连接用户提供的值与将保存的新 User 对象。

protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'country' => $data['country'],
]);
}

您还需要将其添加到register.blade.php以便用户可以提供信息。

<label for="countryInput" class="col-md-4 control-label">Country</label>
<input id="countryInput" type="text" class="form-control" name="country">

最后,如果要验证此字段,则应将其添加到验证器函数(在RegisterController中(。

最新更新