如何在我的新Laravel项目中修复用户注册?

时间:2017-06-13 09:43:31

标签: php mysql database laravel registration

我创建了一个新的Laravel项目。我使用标准php artisan make:auth 并生成所需的文件和连接。我在phpMyAdmin中创建了表,并在 .env 文件中连接了两个表。我检查了连接和迁移,一切运行良好。

问题在于,当我想要创建新用户并输入所有注册数据时,显然无法读取字段first_namelast_name,我收到错误消息& #34;名字字段是必填项。" 姓氏相同。密码或电子邮件没有错误。

首先,这是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文档,但无法发现错误。

1 个答案:

答案 0 :(得分:3)

问题在于:

<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"

说明:

在服务器端,使用element的name属性获取元素值。但在您的情况下,name属性丢失会导致验证失败并显示需要第一个名称字段last_name