Laravel - Session returns null

16,196

In your controller you can save variable into session like

session()->put('user_signup',$user_information);

For checking your session variable in controller

session()->has('user_signup','default value');

For deleting your session variable in controller

session()->forget('user_signup');

For checking your session variable if it exists in blade and printing it out

@if(session()->has('user_signup'))
    session()->get('user_signup')
@endif
Share:
16,196
Bruno Teixeira
Author by

Bruno Teixeira

Updated on June 26, 2022

Comments

  • Bruno Teixeira
    Bruno Teixeira almost 2 years

    I'm using sessions for the first time in Laravel and I'm trying to do a multiple step form, so I thought using sessions would be a smart move. however the following code returns a null value, what am I doing wrong?

            $user_information = [
                "name"           => $request->name,
                "email"          => $request->email,
                "remember_token" => $request->_token,
                "password"       => bcrypt($request->password),
                "role_id"        => 3
            ];
    
            session('user_signup', $user_information);
    
            dd(session('user_signup'));
    
  • Bruno Teixeira
    Bruno Teixeira about 7 years
    whats the difference between session push and put? will try your solution
  • Mostafa Norzade
    Mostafa Norzade almost 5 years
    Thanks. how to get name of user_signup session or every session?
  • Wak
    Wak about 2 years
    Thank you this was my problem, after removing the dd() the session var was finally set.