skip to Main Content

I have done this: https://shouts.dev/articles/how-to-get-online-users-in-laravel
and it works fine.

But I want to get rid of the users that logs out. Otherwise the list will be too large.
This is my lines for log out:

<li>
<a ui-sref="auth.logout">
<img src="/img/loader-blue.gif" class="img-responsive pull-left margin-top-2 margin-right-4 hide" ng-class="{'show': loadingState == 'auth.logout'}" width="17" height="17">
<i class="fa fa-power-off" ng-class="{'hide': loadingState == 'auth.logout'}"></i> {{_('Log out')}}
</a>
</li>

So, here somewhere here I want to "NULL" the column "last_seen" in my users table when the user logs out.

How?

4

Answers


  1. Chosen as BEST ANSWER

    So with help from Roberto Braga I got it to work with this code in UserController:

        $users = User::where('last_seen', '>=', now()->subMinutes(2))
                ->orderBy('last_seen', 'DESC')
                ->paginate(5);
    

    Users disappears after two minutes from the list when not active and reappears when activity rises. Great!


  2. Add users model $fillable = [‘last_seen’,…];

    Login or Signup to reply.
  3. In the logout part of your controller, just update the column to null before logging out the user.

    User::where('id', Auth::user()->id)->update(['last_seen' => null]);
    

    But I think this does not solve your issue, since the user usually just close browser without doing logout.

    If you want to show just people on-line, according to the code people actively browsing your site in the last 2 minute (see the Middleware), just filter in the query in UserController.

    User::where('last_seen', '<=', now()->subMinutes(2))
        ->orderBy('last_seen', 'DESC')
        ->paginate(5);
    
    Login or Signup to reply.
  4. You can try this:

    $id = Auth::id();
    
    $user = User::find($id);
    
    $user->update([
        'column_name' => null
    ]);
    
    Auth::logout(); // logout the user
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search