I have create a middleware where i check for user status(online, ofline).
public function handle(Request $request, Closure $next)
{
if (Auth::check()) {
$expiresAt = now()->addMinutes(1); // keep online for 1 min
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
// Update on db last_online_at with now time
User::where('id', Auth::user()->id)->update(['last_online_at' => now()]);
}
return $next($request);
}
So update also and last_online_at
clumn on db. But what i am trying to do is to see on frontend a status online or ofline. And i have created a model method
// Check if user is online
public function isOnline()
{
return Cache::has('user-is-online-' . $this->id);
}
But i dont know how to pass isOnline
value on vue table
<td>
<span >{{user.last_online_at}}</span>
</td>
<td>
<span >Online</span>
<span >Offline</span>
</td>
methods: {
axios.post('/admin/users/getusers?page=' this.pagination.current_page, {
perPage: this.displayRecord
})
.then(response => {
this.users = response.data.data
}
computed: {
getOnlineStatus() {
var onlineUser = '';
var now = new Date();
this.users.forEach(function(user) {
onlineUser.push(user.last_online_at)
})
console.log(onlineUser);
if(now == onlineUser) {
// want to print true or false i think
}
}
},
laravel controller
public function getUsers(Request $request)
{
abort_if(Gate::denies('user_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$paginate = $request->perPage;
return new UserResource(User::paginate($paginate));
}
CodePudding user response:
I haven't tested this, but is should be something like this.
<td>
<span >{{user.last_online_at}}</span>
</td>
<td>
<span v-if="getOnlineStatus(user.last_online_at)" >Online</span>
<span v-else >Offline</span>
</td>
methods: {
...someMethods,
getOnlineStatus(testDate) {
var myDate = new Date(testDate);
var now = new Date();
let FIVE_MINS = 60 * 5;
return ((now - myDate) < FIVE_MINS);
}
}