Laravel/Eloquent - 时间戳之间的 15 秒间隔

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17221134/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:00:20  来源:igfitidea点击:

Laravel/Eloquent - 15 Second Interval between timestamp

phpmysqllaraveleloquent

提问by cantaffordretail

Trying to prevent users from posting comments too quickly, so using a 15 second interval is the plan.

试图防止用户过快发表评论,所以使用 15 秒的间隔是计划。

This query isn't working to see if a comment was made in the last 15 seconds though. What am I doing wrong?

但是,此查询无法查看是否在过去 15 秒内发表了评论。我究竟做错了什么?

Table:

桌子:

 id  |      comment    |     created_at      |
==============================================
 2   |   blah casd     | 2013-06-20 18:14:17 |

However, I can't quite get it to work with Eloquent

但是,我不能让它与 Eloquent 一起工作

    //are they commenting too fast?
    $protection = DB::table('comments')
       ->where('user_id', '=', $userid)
       ->where('created_at', '<', '(NOW(), INTERVAL 15 SECOND)')
       ->get();

    if(!empty($protection)) {
         return Redirect::back()->with_message('Please wait 15 seconds between comments.', 'error'); 
    }

回答by saran banerjee

Try replacing

尝试更换

->where('created_at', '<', '(NOW(), INTERVAL 15 SECOND)')

with

->where('created_at', '<', DB::raw('NOW() + INTERVAL 15 SECOND)')

I hope this can be of some help.

我希望这能有所帮助。

回答by Marc B

The comma is incorrect. You should be ADDING the interval:

逗号不正确。您应该添加间隔:

NOW() + INTERVAL 15 SECOND

or subtracting, if you change it to a >comparison...

或减去,如果您将其更改为>比较...