就像theHasanov說,這是足夠的超載attemptLogin功能。該EloquentUserProvider建立與給定的憑據查詢:
/**
* 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();
}
,所以我只是分裂輸入的用戶名分成兩個字段和UserProvider作出的休息:
/**
* 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')
);
}
你可以有一個用戶名字段,只是把那個' surname-userId'組合。 – Webinion
你有什麼問題? –
可能能夠應用一個全局範圍,它添加了lastname + id的concat,然後把它放在'username()'函數中,不知道它是否工作。 – Neat