我的HomeController.php中有这段代码,它位于公共函数home()块中
//Fetch Users Online
$online = DB::table('users')->where('online','1')->get();
//Get All Users Online Count
$num_online = DB::table('users')->where('online','1')->count();
//Get All ADMIN Online Count
$admin_online = DB::table('users')->where('online','1')->where('group_id','4')->count();
//Get All MODS Online Count
$mod_online = DB::table('users')->where('online','1')->where('group_id','6')->count();
//Get All VIP Online Count
$vip_online = DB::table('users')->where('online','1')->where('group_id','8')->count();
//Get All UPLOADER Online Count
$uploader_online = DB::table('users')->where('online','1')->where('group_id','7')->count();
//Get All MEMBERS Online Count
$member_online = DB::table('users')->where('online','1')->where('group_id','3')->count();在这里是在home.blade.php中中继的
<div class="col-md-10 col-sm-10 col-md-offset-1">
<div class="clearfix visible-sm-block"></div>
<div class="panel panel-default">
<div class="panel-heading"><h4>Online Users <span class="badge">{{ $num_online }}</span></h4></div>
<div class="panel-body">
@foreach($online as $o)
<span class="badge-user group-member">
{{ $o->username }}
</span>
@endforeach
</div>
<div class="panel-footer">
<div class="row">
<div class="col-sm-12">
<span class="badge-user group-member">Member ({{ $member_online }})</span> <span class="badge-user group-uploader">Uploader ({{ $uploader_online }})</span> <span class="badge-user group-vip">VIP ({{ $vip_online }})</span> <span class="badge-user group-staff">Mod ({{ $mod_online }})</span> <span class="badge-user group-su">Admin ({{ $admin_online }})</span>
</div>
</div>
</div>
</div>
</div>
<!-- /Online Users -->我的问题在我的数据库中的users表中,我有一个名为online的online列。0处于脱机状态,1处于联机状态。如果手动将用户在线状态从0更新为1,则他们会像应该的那样出现在上面所示的Online块中。但是,我如何才能实现自动化呢?如果用户登录,其设置为1,如果用户注销,则其设置为0。
任何帮助都将不胜感激……
我正在运行laravel 4.2,我猜在5.4中会更容易实现这一点。我害怕升级,甚至不想自己尝试。
发布于 2017-02-01 10:40:58
您可以通过创建和侦听事件来实现这一点。
Event::listen('auth.login', function($user)
{
$user->online = 1;
$user->save();
});
Event::listen('auth.logout', function($user)
{
$user->online = 0;
$user->save();
});有关详细信息,请关注laravel documentation
发布于 2017-02-01 19:41:46
你可以在用户会话中检查,当你使用会话的数据库驱动程序时,会有一个名为last_activity的列,如果用户在15分钟内没有做任何事情,你就知道它是离线的。
您不能依赖于用户是否注销。并不是每个人每次都这样做。
https://stackoverflow.com/questions/41967841
复制相似问题