Bear in mind that few users ever logout.
Is this Laravel 8?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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;
}
}
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.
Please or to participate in this conversation.