Search code examples
phpangularjslaravellaravel-9

How to "NULL" a column for a user that logs out, Laravel 9


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?


Solution

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