javascript - 如何设置用户报告之间的时间间隔?

标签 javascript php html laravel

是否可以在用户报告之间添加计时器?我想在用户报告之间添加 5 分钟,或者禁用报告按钮 5 分钟,即使用户刷新页面也是如此。它可能吗?

这是我的举报按钮

<li class="share-link" style="float: right;left: 20px"><a data-toggle="modal" data-target="#exampleModal" href="javascript:void(0)" ><i class="rounded-x icon-flag"></i></a></li>

然后我有一个发送报告的模态。

这是 Controller

   public function careerReportCareerSolution(requ $request)
    {
  $reportExists = \App\Reports::where('user_id', $request['user_id'])
            ->whereDate('created_at', '>', Carbon::now()->subMinutes(5)->toDateTimeString())
            ->exists();

            if($reportExists) {
            // report has been created within 5 minutes
            return Redirect::back()->withErrors(['error', 'Report created within the last 5 minutes']);
        }

        $report = \App\Reports::create([
                        'user_id' => $request['user_id'],
                        'username' => $request['username'],
                        'user_id_posted' => $request['user_id_posted'],
                        'username_posted' => $request['username_posted'],
                        'career_solution_id' =>$request['career_solution_id'],
                        'subject' =>$request['subject'],
                        'why_reporting' =>$request['why_reporting'],
                        'why_reporting_message' =>$request['why_reporting_message'],
                        'additional_message' =>$request['additional_message'],
                        'comment' =>$request['comment'],
                        'comment_user' =>$request['comment_user'],
                        'comment_id' =>$request['comment_id'],
                      ]);
                      $id = $request['career_solution_id']; // looks like this is the ID you ar looking for

                      $career = CareerSolution::findOrfail($id);
                      $career->active = $request['active'];
                      $career->save();



        if($report != ""){
            flash('Career solution report submited', 'success');
        }else{
            flash('Career solution report', 'warning');
        }



        return Redirect::back();


    }


    }

所以,我要设置报告之间的时间,3-5 分钟内用户不能进行报告。

最佳答案

未经测试,但是假设您在 Report 表上使用时间戳(created_atupdated_at),您应该能够通过以下逻辑:

$reportExists = \App\Report::where('user_id', $request['user_id'])
    ->whereDate('created_at', '>', now()->subMinutes(5)->toDateTimeString())
    ->exists();

if ($reportExists) {
    // report has been created within 5 minutes
    return Redirect::back()->withErrors(['error', 'Report created within the last 5 minutes');
}

...

这将使用 ->whereDate() Eloquent 查询方法检查过去 5 分钟内是否为该用户创建了报告。

->exists() 方法用于查明是否至少有一次报告出现。

检查 $reportExists 是否为真。如果是这样,应用程序会将用户重定向到带有错误消息的同一页面(使用适当的消息填充 ->withErrors() 方法)。

这应该放在所有其他 Controller 逻辑之前。

关于javascript - 如何设置用户报告之间的时间间隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58097223/

相关文章:

javascript - React-google-maps 中带孔的多边形

php - Eloquent Laravel 模型上的 __construct

php - 为 capthca 创建不可读的 PHP 页面

javascript - 如果 div 中的子元素在其 Id 中包含字符串,如何触发函数?

css - 在单选按钮上使用 Bootstrap "Collapse"来显示/隐藏内容(无 JS)

javascript - 如何在 Javascript 中添加 JSON 属性?

javascript - jQuery ajax 将事件附加到元素

javascript - 悬停效果在 Safari 中第二次不起作用

Javascript 将 div 移动到标记结束主体 </body>

php - 获取 SQl 语法错误