M
M
Maxim2020-02-18 15:48:04
Laravel
Maxim, 2020-02-18 15:48:04

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);

how do I when searching, if there is a checkbox Online in the request, check if the user is online, if yes, then display only online

$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

1 answer(s)
A
Alexander Aksentiev, 2020-02-18
@Aslero

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 question

Ask a Question

731 491 924 answers to any question