What's the best way to check a user's online status? I found two options, but I need advice to understand which one is better. The option I found: middleware and broadcast
Middleware simply hangs on all web routes and updates the status on each request:
if (Auth::check() && !Cache::has('user-is-online-' . Auth::user()->id)) {
$expiresAt = Carbon::now()->addMinutes(5); // keep online for 5 min
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
User::where('id', Auth::user()->id)->update(['last_seen' => now()]);
}
The middleware method is good, but it uses at least two queries to the database: checking authorization and getting an ID It seems to me that these two requests are too much for middleware.
What method would you recommend?
User::where('id'instead ofAuth::user()?