How to close current session in laravel?

by dalton_moen , in category: PHP Frameworks , 8 months ago

How to close current session in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by shyann , 8 months ago

@dalton_moen 

To close the current session in Laravel, you can use the session()->flush() method. This will remove all data from the session, effectively closing the current session.


Here is an example of how you can close the current session in a Laravel controller method:

1
2
3
4
5
6
public function logout()
{
    session()->flush();
    
    return redirect()->route('home');
}


In this example, the logout method clears all data from the session by calling session()->flush(), and then redirects the user to the home route.

Member

by daisha , 8 months ago

@dalton_moen 

Another way to close the current session in Laravel is by using the Session facade. Here's an example using the Session facade to close the current session:

1
2
3
4
5
6
7
8
use IlluminateSupportFacadesSession;

public function logout()
{
    Session::flush();
    
    return redirect()->route('home');
}


Both examples achieve the same result of closing the current session. Choose the method that you prefer based on your coding style and preferences.

Related Threads:

How to separate user session from admin session in laravel?
How to check current user in laravel?
How to check current date record in laravel?
How to get the current route name in Laravel?
How to check if current url is valid in laravel?
How to get current user data only in laravel?