如何修复新 Laravel 项目的用户注册问题?
How do I fix user registration on my new Laravel project?
我创建了一个新的 Laravel 项目。我使用标准 php artisan make:auth
并且生成了所需的文件和连接。我在 phpMyAdmin 中创建了 table 并在 .env 文件中连接了两者。我检查了连接和迁移,一切正常。
问题是,当我想创建一个新用户并输入所有注册数据时,字段 first_name
和 last_name
显然无法读取,我收到一条错误消息 "The first name field is required." 与姓氏相同。密码或电子邮件没有错误。
首先,这是 RegisterController:
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|string|max:20',
'last_name' => 'required|string|max:30',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
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']),
]);
}
register.blade.php:
<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
<label for="first_name" class="col-md-4 control-label">First Name</label>
<div class="col-md-6">
<input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>
@if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
<label for="last_name" class="col-md-4 control-label">Last Name</label>
<div class="col-md-6">
<input id="last_name" type="text" class="form-control" last_name="last_name" value="{{ old('last_name') }}" required autofocus>
@if ($errors->has('last_name'))
<span class="help-block">
<strong>{{ $errors->first('last_name') }}</strong>
</span>
@endif
</div>
</div>
和迁移:
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name', 20);
$table->string('last_name', 30);
$table->string('email')->unique();
$table->string('password', 30);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
我检查了论坛和 Laravel 文档,但没有发现错误。
问题在这里:
<input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>
在这里而不是name="first_name"
,first_name="first_name"
在那里。使其正确并重试。 last_name="last_name"
也一样。
解释:
在服务器端,使用元素的 name
属性获取元素值。但是在你的情况下 name
属性丢失导致验证失败并且它显示 名字字段是必需的 和 last_name
我创建了一个新的 Laravel 项目。我使用标准 php artisan make:auth
并且生成了所需的文件和连接。我在 phpMyAdmin 中创建了 table 并在 .env 文件中连接了两者。我检查了连接和迁移,一切正常。
问题是,当我想创建一个新用户并输入所有注册数据时,字段 first_name
和 last_name
显然无法读取,我收到一条错误消息 "The first name field is required." 与姓氏相同。密码或电子邮件没有错误。
首先,这是 RegisterController:
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|string|max:20',
'last_name' => 'required|string|max:30',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
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']),
]);
}
register.blade.php:
<form class="form-horizontal" role="form" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('first_name') ? ' has-error' : '' }}">
<label for="first_name" class="col-md-4 control-label">First Name</label>
<div class="col-md-6">
<input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>
@if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('last_name') ? ' has-error' : '' }}">
<label for="last_name" class="col-md-4 control-label">Last Name</label>
<div class="col-md-6">
<input id="last_name" type="text" class="form-control" last_name="last_name" value="{{ old('last_name') }}" required autofocus>
@if ($errors->has('last_name'))
<span class="help-block">
<strong>{{ $errors->first('last_name') }}</strong>
</span>
@endif
</div>
</div>
和迁移:
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name', 20);
$table->string('last_name', 30);
$table->string('email')->unique();
$table->string('password', 30);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
}
我检查了论坛和 Laravel 文档,但没有发现错误。
问题在这里:
<input id="first_name" type="text" class="form-control" first_name="first_name" value="{{ old('first_name') }}" required autofocus>
在这里而不是name="first_name"
,first_name="first_name"
在那里。使其正确并重试。 last_name="last_name"
也一样。
解释:
在服务器端,使用元素的 name
属性获取元素值。但是在你的情况下 name
属性丢失导致验证失败并且它显示 名字字段是必需的 和 last_name