2017-06-13 54 views
1

我创建了一个新的Laravel项目。我使用标准php artisan make:auth 并生成了所需的文件和连接。我在phpMyAdmin中创建了这个表,并在.env文件中连接了这两个表。我检查了连接和迁移,所有工作都很好。如何修复我的新Laravel项目的用户注册?

的问题是,当我想创建一个新用户,并进入所有的寄存器数据,字段first_namelast_name显然不能被读取,我得到一个错误说“要求第一名称字段。”相同的姓氏。没有密码或电子邮件错误。

首先,这里的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文档,但couldn” t发现一个错误。

回答

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

说明:

在服务器端,元件值被取出使用元件的name属性。但在你的情况下name属性丢失,导致验证失败,它显示第一个名称字段是必需的和相同的last_name