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 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程