In Laravel display if the user is online?

if the user is online, the cache record id

$expiresAt = Carbon::now()->addMinutes(5);
 Cache::put('user-is-online' . Auth::user()->id, true, $expiresAt);

how can I when searching, if the Online checkbox in the query to check whether the user is online, if Yes, print 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')

return response()->json($users);
April 7th 20 at 15:41
1 answer
April 7th 20 at 15:43
of course does not work, you will have stopitsot queries for each user and get necessary each time the entire database to check whether the online user.

You just have to add some last_activity_at and on every request the user to set there the current date.

When you select already choose those who have last_activity_at > now()->subMinutes(5) for example.

This will be online users.
in the table of users last_activity_at ? - Astrid.White commented on April 7th 20 at 15:46
@fabian.Vandervort, Yes - Alvina_Ernser commented on April 7th 20 at 15:49

Find more questions by tags Laravel