2016-06-12 155 views
0

我是Laravel的新手,我正在開發一個應用程序,要求用戶使用電子郵件(少數)或其電話號碼(多數)登錄。目前,我使用默認的Laravel身份驗證和修改後的註冊來允許用戶提供他們的電話號碼。我找到的解決方案顯示如何使用用戶名登錄而不是電子郵件。我怎樣才能做到這一點?我運行Laravel 5.2與PHP 7.0用Laravel 5.2的電子郵件地址或電話號碼登錄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> 

回答

0

在您的loginUsername函數中,進行以下更改以檢查請求是否有使用電子郵件默認設置之前的電話屬性。

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

+0

的'loginUsername()'方法沒有訪問'$ request'你有它存在的方式 – jszobody

+0

因爲它是目前你可以通過請求實例'loginUsername'方法的調用方法'AuthenticateUsers'類。 –

+0

我已將'$ request'參數傳遞給'loginUsername()'函數,並在ThrottlesLogins.php中將其更改爲看起來像這樣的'protected function getThrottleKey(Request $ request) { return mb_strtolower($ request-> input ($這 - > loginUsername($請求))) '|' $請求 - >的IP();。 }'。有了這個,我可以用我的電子郵件地址登錄。如何修改我的**視圖/模型/控制器**以接受電話輸入? @Oluwafemi – Ben100

相關問題