首页 文章

使用Laravel 5.2中的电子邮件或电话登录

提问于
浏览
0

我是Laravel的新手,我正在开发一个应用程序,要求用户使用电子邮件(少数)或电话号码(大多数)登录 . 目前我正在使用默认的Laravel身份验证和修改注册,以允许用户提供他们的电话号码 . 我找到的解决方案显示了如何使用用户名而不是电子邮件登录 . 我怎么能实现这个目标?我用PHP 7.0运行Laravel 5.2

这是我的用户模型:

protected $fillable = ['firstname', 'lastname', 'email','phone','account_type','county','sub_county', 'password',];

protected $hidden = ['password', 'remember_token',];

这是我的AuthenticateUsers.php:

/**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function getCredentials(Request $request)
{
    return $request->only($this->loginUsername(), 'password');
}

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required', 'password' => 'required',
    ]);
}

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function loginUsername()
{
    return property_exists($this, 'username') ? $this->username : 'email';
}

登录表单中的电子邮件输入字段:

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="text" class="form-control" name="email" value="{{ old('email')    }}">

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

1 回答

  • 0

    loginUsername 函数中,进行以下更改以在使用电子邮件默认值之前检查请求是否具有电话属性 .

    public function loginUsername($request) { return $request->has('phone') ? 'phone' : 'email'; }

相关问题