I have Laravel app where i check user loggedin regularly with a heartbeat for every 3 seconds (demo purpose, actually 5 minutes). For each beat I check if user\'s last activity
The accepted answer is correct, but you could make it even cleaner by using a query scope on the User model (assuming that you have a User model)...
$result = User::currentUser()->activityOlderThan(self::HEARTBEAT_INTERVAL)->get();
Your User model would then have these functions:
public function scopeCurrentUser($query)
{
return $query->where('id_user', '=', Session::get('id_user'));
}
And
public function scopeActivityOlderThan($query, $interval)
{
return $query->where('last_activity', '>=', Carbon::now()->subMinutes($interval)->toDateTimeString());
}
Now your code is clean and easy to read :-)