我是 Laravel 的初学者。我在我的项目中使用 Laravel 5.8。我有这个模型:class User extends Authenticatable implements MustVerifyEmail{ use Notifiable; public static $roles = []; protected $fillable = ['last_activity', 'enable', 'name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'isCompany', 'isMailing']; protected $hidden = [ 'password', 'remember_token', ]; public function roles() { return $this->belongsToMany('App\Role'); } public function mainRole() { return $this->hasOne('App\Role'); } public function comments() { return $this->hasMany('App\Comments'); } public function hasRole(array $roles) { foreach ($roles as $role) { if (isset(self::$roles[$role])) { if (self::$roles[$role]) return true; } else { self::$roles[$role] = $this->roles()->where('name', $role)->exists(); if (self::$roles[$role]) return true; } } return false; } public function loginHistory() { return $this->hasMany('App\UserLoginHistory'); } public function companies() { return $this->belongsTo('App\Companies', 'company_id'); } public function images() { return $this->hasManyThrough('App\UploadedFiles', 'App\User', 'id', 'photoable_id'); } public function scopeOfRoleType($query, $types) { return $query->whereHas('roles', function ($q) use ($types) { $q->whereIn('name', $types); }); }}在 last_activity - 我有日期,例如 2019-07-16 10:26:12我需要在我的树枝页面上显示:@foreach ($usersList as $user)User name: $user->name is now ...... (offline or online)@endforeach我想看看用户是在线还是离线。我想这样的事情:public function isOnline() { return ..... }但我不知道如何检查此活动:(
2 回答
米脂
TA贡献1836条经验 获得超3个赞
这取决于您如何定义在线。Http 是一种无状态协议。
但是,您可以将在线定义为在过去五分钟内处于活动状态。这样你就可以实现这样的功能:
public function isOnline(): bool
{
return $this->last_activity->gt(Carbon\Carbon::now()->subMinutes(5));
}
还要确保将 last_activity 标记为日期:
protected $dates = ['last_activity'];
随着您的应用程序的增长,我会考虑另一种解决方案,而不是将所有用户从数据库加载到内存中。
温温酱
TA贡献1752条经验 获得超4个赞
我想“在线”是指使用网站并与之互动。您必须确定一个时间间隔,在该时间间隔内,用户可以被视为“在线”或“离线”并且不再进行交互。也许……10分钟?将“上次活动”字段与当前时间进行比较,以查看它们是否处于活动状态。用于示例Carbon::now()获取当前时间并进行比较。
- 2 回答
- 0 关注
- 232 浏览
添加回答
举报
0/150
提交
取消