This is a wrong approach, you need to use websockets not a server side code. Laravel has an official library called Echo, you need to install it and use a websocker server like pusher or socket.it and then you can check online status or availability with Presence channel. Check this: https://laravel.com/docs/8.x/broadcasting#presence-channels
Nov 9, 2021
4
Level 1
How to check is user is Online through API
I have this in my Middleware
public function handle($request, Closure $next)
{
if(Auth::check()){
$expiresAt = Carbon::now()->addMinutes(1); // keep online for 1 min
Cache::put('user_is_Online' . Auth::user()->id, true, $expiresAt);
// last seen
User::where('id', Auth::user()->id)->update(['last_seen' => now()]);
}
return $next($request);
}
and this in my USER model
public function isOnline(){
return Cache::has('user_is_Online'.$this->id);
}
how do i return all users wether online or not and add a value to identify online users from offline users using api
public function getusers($msg){
$users = User::withBasic()->where('usertype_id' , 4)->orderBy('created_at' , 'DESC')->paginate(12);
return response()->json([
'res' => $users,
'message'=>$msg,
'status' => 200
], 200);
}
Please or to participate in this conversation.