Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在laravel 5.2中向登录函数添加到期日条件_Laravel_Laravel 5.2 - Fatal编程技术网

如何在laravel 5.2中向登录函数添加到期日条件

如何在laravel 5.2中向登录函数添加到期日条件,laravel,laravel-5.2,Laravel,Laravel 5.2,在Laravel5.2中,我想添加一个条件,以便只有到期日大于今天日期的用户才能登录 protected function getCredentials(Request $request) { return ['email' => $request->{$this->loginUsername()}, 'password' => $request->password]; } 代码不接受添加: “过期”=>gte(碳::现在() 非常感谢任何帮助我认为这是不可能

在Laravel5.2中,我想添加一个条件,以便只有到期日大于今天日期的用户才能登录

protected function getCredentials(Request $request)
{

return ['email' => $request->{$this->loginUsername()}, 'password' => $request->password];

}
代码不接受添加: “过期”=>gte(碳::现在()


非常感谢任何帮助

我认为这是不可能的,即使是在Laravel 5.5中。查看
light\Auth\EloquentUserProvider
中用于从数据库获取用户的
retrieveByCredentials
方法,可以看到查询将简单的键/值组合传递到
$query
对象上的
where
方法,该方法等同于
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.
}
我不确定5.2中是否提供了
auth()
帮助程序,但您应该能够使用
auth
外观执行相同的操作,例如
auth::trust(…)