2015-05-09 78 views
2

我是Laravel 5的新手,我使用它內置的登錄內容,是的,我可以用電子郵件和密碼登錄,一切正常。現在,我創建了一個名爲username的新列,並在用戶表(包含登錄內容的表)中填充了我所需的用戶名。我如何使用電子郵件或用戶名和密碼登錄?用郵箱或用戶名登錄

我發現這個從AuthenticatesAndRegistersUsers.php

/** 
* Handle a login request to the application. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function postLogin(Request $request) 
{ 
    $this->validate($request, [ 
     'email' => 'required|email', 'password' => 'required', 
    ]); 

    $credentials = $request->only('email', 'password'); 

    if ($this->auth->attempt($credentials, $request->has('remember'))) 
    { 
     return redirect()->intended($this->redirectPath()); 
    } 

    return redirect($this->loginPath()) 
       ->withInput($request->only('email', 'remember')) 
       ->withErrors([ 
        'email' => $this->getFailedLoginMessage(), 
       ]); 
} 

任何建議,建議,線索,思路,有利於調整上面的代碼,這樣我可以用任何用戶名或電子郵件地址和密碼登錄?

回答

1

documentation指出可以使用自定義列。所以,如果你想用一個用戶名進行身份驗證,你可以使用以下命令:

this->auth->attempt(['username' => 'the_username', 'password' => 'the_password', 
$request->has('remember')); 

這就需要你,如果第一個失敗做驗證第二個電話::嘗試。

你也可以採取在回答到a similar question(感謝拉斐爾的出色解決辦法:

​​
+0

謝謝大家的答案,但沒有工作 –

+0

我終於得到了它的工作謝謝! –

相關問題