在laravel 5.2中,我想添加条件,以便只有用户的有效期限大于今天的登录日期。
protected function getCredentials(Request $request)
{
return ['email' => $request->{$this->loginUsername()}, 'password' => $request->password];
}
代码不接受添加: '到期' => GTE(碳::现在())
感谢任何帮助
答案 0 :(得分:2)
即使在Laravel 5.5中,我也不认为这是可能的。看一下retrieveByCredentials
中用于从数据库中获取用户的Illuminate\Auth\EloquentUserProvider
方法,您可以看到查询将简单的键/值组合传递给where
方法$query
对象,等同于where key = value
。这是从5.5:
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials) ||
(count($credentials) === 1 &&
array_key_exists('password', $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();
}
为了实现您的目标,我建议您在用户登录之后检查,例如在您的控制器中:
// Imagine this is the controller method where you're dealing with user logins
public function login(array $credentials)
{
if (! auth()->attempt($credentials)) {
// Handle what happens if the users credentials are incorrect.
}
$user = auth()->user();
if (Carbon::now()->gte($user->expires)) {
// User's account has expired, lets log them out.
auth()->logout();
// Return a redirect with a message or something...
}
// Handle a successful login.
}
我不确定auth()
帮助程序是否在5.2中可用,但您应该可以使用Auth
外观来执行相同的操作,例如Auth::attempt(...)
。