Home > Back-end >  How to detect laravel logout time
How to detect laravel logout time

Time:09-07

someone knows how to record the user's logout time in the database. Laravel. I made the entry time, I don’t understand how to pick up the exit time.

CodePudding user response:

You can listen Logout event and update field.

First - create a Listener:

php artisan make:listener UpdateLogoutTime

Than, register it in EventServiceProvider

use App\Listeners\UpdateLogoutTime;
use Illuminate\Auth\Events\Logout;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        Logout::class => [
            UpdateLogoutTime::class
        ]
    ];
}

Than, make update logic in listener:

class UpdateLogoutTime
{
    public function handle(Logout $event)
    {
        $user = $event->user;
        //update time logic
    }
}

CodePudding user response:

You can do like this:

Auth::user()->update([ 'logout_time' => now() ]);
Auth::logout();

Or,

$id = Auth::id();

if(Auth::logout()) {
    User::where('id', $id)->update([ 'logout_time' => now() ]);
}
  • Related