drfals1307 2018-02-09 16:22
浏览 82
已采纳

修改默认的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?

  • 写回答

1条回答 默认 最新

  • drlq92444 2018-02-09 16:28
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据