php Laravel 选择超过 5 分钟的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19475896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel select records older than 5 minutes?
提问by user2002495
I have Laravel app where i check user loggedin regularly with a heartbeat for every 3 seconds (demo purpose, actually 5 minutes). For each beat I check if user's last activity with current time is also 5 minutes. If it is, log them out.
我有 Laravel 应用程序,我每 3 秒检查一次用户登录情况(演示目的,实际上是 5 分钟)。对于每个节拍,我检查用户与当前时间的最后一次活动是否也是 5 分钟。如果是,请将它们注销。
Here's my code:
这是我的代码:
$result = DB::table('db_user')->where('id_user','=',Session::get('id_user'))->where('last_activity','>=',"now() - interval 5 minute")->get();
if(!empty($result))
return Redirect::to('/logout');
else
return Response::json('still-alive');
My problem is, this doesn't work. If I change the operand to <=
, it will log me out immediately before 5 minutes, if the operand is >=
, even after 5 minutes it won't log me out, can anyone explain why?
我的问题是,这不起作用。如果我将操作数更改为<=
,它会在 5 分钟前立即将我注销,如果操作数是>=
,即使在 5 分钟后它也不会将我注销,谁能解释为什么?
-EDIT-
-编辑-
Thanks for all the answers, I solved my problems by modifying my code to:
感谢所有的答案,我通过修改我的代码解决了我的问题:
$result = DB::table('db_user')->where('id_user','=',Session::get('id_user'))->first();
if(!empty($result))
if(strtotime($result->last_activity) < strtotime("-5 minutes"))
return Redirect::to('/logout');
else
return Response::json('still-alive');
else
return Response::json('no-record');
回答by Ben Wilkins
The accepted answer is correct, but you could make it even cleaner by using a query scope on the User model (assuming that you have a User model)...
接受的答案是正确的,但您可以通过在 User 模型上使用查询范围使其更清晰(假设您有一个 User 模型)...
$result = User::currentUser()->activityOlderThan(self::HEARTBEAT_INTERVAL)->get();
Your User model would then have these functions:
您的 User 模型将具有以下功能:
public function scopeCurrentUser($query)
{
return $query->where('id_user', '=', Session::get('id_user'));
}
And
和
public function scopeActivityOlderThan($query, $interval)
{
return $query->where('last_activity', '>=', Carbon::now()->subMinutes($interval)->toDateTimeString());
}
Now your code is clean and easy to read :-)
现在您的代码干净且易于阅读:-)
回答by Glad To Help
Assuming your business logic is correct, try using PHP instead of an SQL string in the where
clause:
假设您的业务逻辑是正确的,请尝试在where
子句中使用 PHP 而不是 SQL 字符串:
$date = new DateTime;
$date->modify('-5 minutes');
$formatted_date = $date->format('Y-m-d H:i:s');
$result = DB::table('db_user')->where('id_user','=',Session::get('id_user'))->where('last_activity','>=',$formatted_date)->get();
Also, it is a good practice to always output the executed SQL queries just to make sure Laravel behaves as expected:
此外,为了确保 Laravel 的行为符合预期,始终输出已执行的 SQL 查询是一个好习惯:
$queries = DB::getQueryLog();
回答by user570605
You can use whereRaw()
:
您可以使用whereRaw()
:
$result = DB::table('db_user')->where('id_user','=',Session::get('id_user'))->whereRaw('last_activity >= now() - interval 5 minute')->get();
or
或者
$result = DB::table('db_user')->where('id_user','=',Session::get('id_user'))->whereRaw('last_activity >= now() - interval ? minute', [5])->get();