简体   繁体   中英

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?

You can try this:

$id = Auth::id();

$user = User::find($id);

$user->update([
    'column_name' => null
]);

Auth::logout(); // logout the user

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

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!

Add users model $fillable = ['last_seen',...];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM