是否可以在用户报告之间添加计时器?我想在用户报告之间添加 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_at
、updated_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/