2017-05-29 11 views
0

laravelにログインする必要があります。通常は問題ありません。しかし、私の顧客は特別なユーザー名でログインしたいと思っています。それはsurname-userIdでなければなりません。laravel authentication:複合ユーザー名を使用する

ので、ユーザのテーブルがあります:

id; firstname; lastname; email; ... 
1; testuser1; Smith; [email protected]; 

ここでログイン名が "スミス-1" でなければなりません。私は、LoginControllerの関数username()をオーバーライドするソリューションを見つけました。しかし、この場合、私は2つのテーブルフィールドを組み合わせてユーザー名を構築する必要があります。

これまでに誰かが行ったことはありますか?

+0

を使用すると、1つのユーザ名フィールドを持っており、ちょうど 'ことを置くことができますので、私はちょうど2つのフィールドに入力されたユーザ名を分割さとUserProviderが残りを作っ

/** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { if (empty($credentials)) { return; } // First we will add each credential element to the query as a where clause. // Then we can execute the query and, if we found a user, return it in a // Eloquent User "model" that will be utilized by the Guard instances. $query = $this->createModel()->newQuery(); foreach ($credentials as $key => $value) { if (! Str::contains($key, 'password')) { $query->where($key, $value); } } return $query->first(); } 

surname-userId'の組み合わせがあります。 – Webinion

+0

あなたの問題は何ですか? –

+0

lastname + idのconcatを追加するグローバルスコープを適用できるかもしれませんが、 'username()'関数に入れてください。 – Neat

答えて

0

は、attemptLogin機能をオーバーロードするのに十分であると述べました。 EloquentUserProviderは、与えられた資格情報を使用してクエリを構築します。

/** 
* Attempt to log the user into the application. 
* 
* @param \Illuminate\Http\Request $request 
* @return bool 
*/ 
protected function attemptLogin(Request $request) 
{ 
    $credentials = $this->credentials($request); 

    list($lastname, $id) = explode('-', $credentials[$this->username()]); 

    $params = [ 
     'id' => (int) $id, 
     'name' => $lastname, 
     'password' => $credentials['password'], 
    ]; 

    return $this->guard()->attempt(
     $params, $request->has('remember') 
    ); 
} 
1

だけ過負荷方法theHasanov同様attemptLogin

+0

その後、私は自分ですべての認証を受けるでしょう –

+0

あなたは正しいです! attemptLogin関数を上書きするだけで十分でした。私は複雑に思っていた。私はより詳細な回答を投稿します –

関連する問題