Laravel 8 Jetstream:在注册过程中添加新字段
Laravel 8 Jetstream: adding new field to the registration process
我开始使用 Laravel 8 构建 Web 应用程序。我注意到 Laravel 8 中发生了很多变化,包括身份验证。现在,我正在尝试使用 Jetstream 进行身份验证。
我有运行以下命令将其集成到应用程序中。
composer require laravel/jetstream
php artisan jetstream:install inertia
npm install && npm run dev
当我转到 /register
路线时,我可以看到带有姓名、电子邮件、密码和密码确认字段的注册表。我想做的是我想添加另一个名为“公司”的字段,我想对其应用自定义验证规则。
我在 Jetstream 文档中发现我可以在 JetstreamServiceProvider class 的引导方法中自定义身份验证过程,如下所示。
Fortify::authenticateUsing(function (Request $request) {
});
但不适用于注册。如何自定义添加新字段和应用自定义验证规则的注册过程?
首先,您应该使用 database\migrations14_10_12_000000_create_users_table.php
中找到的迁移将 company
字段添加到用户 table。
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('company');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->text('profile_photo_path')->nullable();
$table->timestamps();
});
}
然后 运行 此命令 php artisan migrate:fresh
迁移您的新用户 table。
然后将字段添加到 \app\Models\User.php
中找到的 User
模型中的可填充数组,如下所示:
protected $fillable = [
'name',
'company',
'email',
'password',
];
现在您可以在 resources\views\auth\register.blade.php
下找到注册视图,然后您可以复制一个输入块以将其用于 'company'
字段。
然后你可以在这个class中做验证:app\Actions\Fortify\CreateNewUser.php
public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'company' => ['required', 'string', 'max:255'],
'password' => $this->passwordRules(),
])->validate();
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'company' => $input['company'],
'password' => Hash::make($input['password']),
]);
}
那么,你准备好了。
@Ali Ali 的回答是正确的,但是如果对于那些使用 Inertia 的人来说还有一个额外的步骤。您需要打开 resources/js/Pages/Auth/Register.vue
并将所需的字段添加到表单中。此外,向下滚动到文件底部并将字段名称添加到 this.$inertia.form
输入参数对象文字。例如,如果所需的附加字段名称是 company
,您将像这样添加它:
data() {
return {
form: this.$inertia.form({
name: '',
company: '', // <- New line here
email: '',
password: '',
password_confirmation: '',
terms: false,
})
}
},
我开始使用 Laravel 8 构建 Web 应用程序。我注意到 Laravel 8 中发生了很多变化,包括身份验证。现在,我正在尝试使用 Jetstream 进行身份验证。
我有运行以下命令将其集成到应用程序中。
composer require laravel/jetstream
php artisan jetstream:install inertia
npm install && npm run dev
当我转到 /register
路线时,我可以看到带有姓名、电子邮件、密码和密码确认字段的注册表。我想做的是我想添加另一个名为“公司”的字段,我想对其应用自定义验证规则。
我在 Jetstream 文档中发现我可以在 JetstreamServiceProvider class 的引导方法中自定义身份验证过程,如下所示。
Fortify::authenticateUsing(function (Request $request) {
});
但不适用于注册。如何自定义添加新字段和应用自定义验证规则的注册过程?
首先,您应该使用 database\migrations14_10_12_000000_create_users_table.php
中找到的迁移将 company
字段添加到用户 table。
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('company');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->text('profile_photo_path')->nullable();
$table->timestamps();
});
}
然后 运行 此命令 php artisan migrate:fresh
迁移您的新用户 table。
然后将字段添加到 \app\Models\User.php
中找到的 User
模型中的可填充数组,如下所示:
protected $fillable = [
'name',
'company',
'email',
'password',
];
现在您可以在 resources\views\auth\register.blade.php
下找到注册视图,然后您可以复制一个输入块以将其用于 'company'
字段。
然后你可以在这个class中做验证:app\Actions\Fortify\CreateNewUser.php
public function create(array $input)
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'company' => ['required', 'string', 'max:255'],
'password' => $this->passwordRules(),
])->validate();
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'company' => $input['company'],
'password' => Hash::make($input['password']),
]);
}
那么,你准备好了。
@Ali Ali 的回答是正确的,但是如果对于那些使用 Inertia 的人来说还有一个额外的步骤。您需要打开 resources/js/Pages/Auth/Register.vue
并将所需的字段添加到表单中。此外,向下滚动到文件底部并将字段名称添加到 this.$inertia.form
输入参数对象文字。例如,如果所需的附加字段名称是 company
,您将像这样添加它:
data() {
return {
form: this.$inertia.form({
name: '',
company: '', // <- New line here
email: '',
password: '',
password_confirmation: '',
terms: false,
})
}
},