Answer the question
In order to leave comments, you need to log in
How to display if the user is online in Laravel?
if the user is online, then I write his id to the cache
$expiresAt = Carbon::now()->addMinutes(5);
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
$users = User::with('attributes');
if ($request->has('online') && $request->online == 1){
}
$users->where('id','!=', Auth::user()->id);
$users = $users ->orderBy('date_top','desc')
->orderBy('date_premium','desc')
->orderBy('id','desc')
->get();
return response()->json($users);
Answer the question
In order to leave comments, you need to log in
it won’t work out naturally, you will have a hundred requests for each user and you need to get the entire database every time to check if the user is online.
You just need to add some last_activity_at and put the current date there with each user request.
When choosing, you already select those who have last_activity_at > now()->subMinutes(5) for example.
These will be online users.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question