Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

scottsuhy's avatar

How to do a query prior to user logging out (but triggered by logout)

I need to do a DB query prior to the user logging out in order to determine what page to send them to upon logout.

I thought putting the following in the LoginController would work but by the time this function overrides AuthenticatesUsers@loggedOut() the user session is already terminated so auth()->user() is not available.

Is there a risk in putting code in the AuthenticateUsers@logout() function? Is it possible to override logout()--if so how?

Is there a better way to do this?

protected function loggedOut (Request $request) {

        $store_owner = DB::table('users')->select('referrer')
            ->where('email', auth()->user()->email)
            ->first();

        if(is_null($store_owner->referrer)){
            return RouteServiceProvider::HOME;
        }
        else{
            return 'https://icollect.money/stores_user/'.$store_owner->referrer;
        }
}
0 likes
5 replies
Snapey's avatar

Bear in mind that few users ever logout.

Is this Laravel 8?

scottsuhy's avatar

Laravel Framework 6.18.0

I need to upgrade but it's still on the backlog

Snapey's avatar
Snapey
Best Answer
Level 122

Create your own controller and setup a logout route that receives the post request

The Laravel 8 Logout function looks like this

    public function destroy(Request $request): LogoutResponse
    {
        $this->guard->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();

        return app(LogoutResponse::class);
    }

copy this in your own logout controller, but decide on the route to redirect to before destroying the session

    public function destroy(Request $request)
    {
	$store_owner = auth()->user();

        $this->guard->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();


        if(is_null($store_owner->referrer)){
            return RouteServiceProvider::HOME;
        }
        else {
            return 'https://icollect.money/stores_user/'.$store_owner->referrer;
        }
    }

That icollect url. Is this the laravel site? Think very hard about coding urls into your code. Use relative urls or include from a env/config file so that you can change the hostname without having to redeploy any code.

scottsuhy's avatar

Thank you very much. and great advice on the URL. I'll fix that now as well.

scottsuhy's avatar

worked great. a couple of changes because it's Laravel 6.18

  1. destroy() to logout()
  2. $this->guard to $this->guard()
  3. return to return redirect(URL)

you guys rock!

public function logout(Request $request)
    {
	$store_owner = auth()->user();

        $this->guard()->logout();

        $request->session()->invalidate();

        $request->session()->regenerateToken();


        if(is_null($store_owner->referrer)){
            return RouteServiceProvider::HOME;
        }
        else {
            return redirect( 'https://icollect.money/stores_user/'.$store_owner->referrer);
        }
    }

Please or to participate in this conversation.