Home > Software design >  How to "NULL" a column for a user that logs out, Laravel 9
How to "NULL" a column for a user that logs out, Laravel 9

Time:10-05

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"  ng- width="17" height="17">
<i  ng-></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?

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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!

CodePudding user response:

You can try this:

$id = Auth::id();

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

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

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