Adil
Adil

Reputation: 27

how create and save session value after logout

i am creating session for some purpose but when user logout the purpose value becomes null but i want to use it after user logout.. the scenario is the session is created by admin and i want use this session for normal user but when admin logout its session also become null..

this is the logout code of laravel

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

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

        return redirect('/');
    }

Upvotes: 1

Views: 1440

Answers (1)

Amr Aly
Amr Aly

Reputation: 3905

Here's what you can do:

first get all the data you want to keep.

then delete all the session data.

then save the data in to the session.

then logout.

 public function logout(Request $request)
 {

 // get the data first for example the user's name
 $name = Auth::user()->name;
 $this->guard()->logout();
 $request->session()->invalidate();

 // save the data into a new session
 session(['name' => $name]);


 return redirect('/');
 }

then in your view you get the data like so:

@if(session('name'))
  {{ session('name') }}
@endif

Upvotes: 1

Related Questions