我正在尝试找出一种方法来计算自网站管理员上次登录以来数据库中的特定表中添加了多少条目。
当我创建管理面板时,我需要找出自管理员上次登录管理面板以来有多少订阅请求到达了数据库表。
我只想显示收到的请求数? 我正在使用 Laravel 和 mysql 作为数据库。
最佳答案
创建一个表来存储静态数据:
id | count | readed
向您的订阅请求模型添加一个监听器,在每个 Creation Event
上触发(将其添加到提供程序中):
SubscriptionRequest::created(function ($data) {
//look for last statics record
$last_static_unread = Statics::where('readed',0)->first();
//if there aren't any record is our first request since last login
if(is_null($last_static_unread)){
$last_static_unread = new Statics();
$last_static_unread->count = 1;
$last_static_unread->readed = 0;
$last_static_unread->save();
}else{
$last_static_unread += 1;
$last_static_unread->update();
}
});
然后在您的管理面板中获取并更新它,例如在 Controller 中为主要静态 View 或其他内容提供服务:
public function getTotalRequestSinceLastVisit (){
$last_statics = Statics::where('readed',0)->first()
$total_requests = 0;
if(!is_null($last_statics)){
$total_requests = $last_statics->count;
$last_statics->readed = 1;
$last_statics->update();
}
return $total_requests;
}
关于php - 如何在 Laravel 的特定表中找到自用户上次登录以来的数据库条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46842538/