[英]Check if token expired in Laravel
I have table tokens with columns: user_id
, token
, expires_at
. 我有带有列的表令牌:
user_id
, token
, expires_at
。
Example: 例:
I have token: 12345, for me, and he expires at: 2018-06-05 我有令牌:12345,对我来说,他过期于:2018-06-05
When I generate new token, I generate up to 7 days.. 生成新令牌时,最多生成7天。
How I can check this in model? 我该如何检查模型?
I tryied do with scope in model: 我试着做模型的范围:
public function scopeExpired($query) {
return $this->where('expires_at', '<=', Carbon::now())->exists();
}
But not working. 但是没有用。 Always false..
总是假..
I've always done stuff like this the following way. 我一直按照以下方式做这样的事情。 Note that you need the
expires_at
field as an attribute on your model. 请注意,您需要将
expires_at
字段作为模型上的属性。
// Probably on the user model, but pick wherever the data is
public function tokenExpired()
{
if (Carbon::parse($this->attributes['expires_at']) < Carbon::now()) {
return true;
}
return false;
}
Then from wherever you can call: 然后,您可以从任何地方致电:
$validToken = $user->tokenExpired();
// Or realistically
if ($user->tokenExpired()) {
// Do something
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.