修改默认的Laravel身份验证

修改默认的Laravel身份验证

问题描述:

I imported Laravels authentication using

Php artisan make: auth

I'm trying to add an additional field to the registration page. The new field is a drop-down with two options of user type. Landlord/Tenant. When I click register, I'm redirected to the login script, with all my details still there, and the registration hasn't gone through. Anywhere else there might be an issue,

I have updated my model and remigrated it. I have also added it to the fillable array in the user class.

Register Blade Template

                <div class="form-group row">
                  <label for="user-type" class="col-md-4 col-form-label text-md-right">User Type</label>
                    <div class="col-md-6">
                      <select class ="form-control" id="user-type">
                        <option>Landlord</option>
                        <option>Tenant</option>
                      </select>
                      @if ($errors->has('user-type'))
                          <span class="invalid-feedback">
                              <strong>{{ $errors->first('email') }}</strong>
                          </span>
                      @endif
                    </div>
                </div>

Register Controller

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'user-type' => 'required|string',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'user-type' => $data['user-type']
    ]);
}

User Class

protected $fillable = [
        'name', 'email', 'password', 'user-type'
    ];

User Migration

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('user-type');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

As you can see I have added user-type to all the places I think it needs to be added, am I missing somewhere?

When creating HTML elements, the thing that is being sent along in the request are values, those values are being keyed by the attribute called name on each field, and as you can see your select is missing that attribute, try:

<select class="form-control" name="user-type">

To test this out, you can put dd($data); inside create method, just before you create a new user