7

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'));
4
  • what Laravel version are you using and where do you store your sessions? Commented Feb 14, 2017 at 18:16
  • I guess i'm using the latest version, I didn't tweak my setting so it should be on the "file" driver Commented Feb 14, 2017 at 18:30
  • 1
    Getting a session item is with session(key, default) setting a session is with session([key => value]) Commented Feb 14, 2017 at 18:32
  • @apokryfos it worked, such a rookie mistake. Thank you very much Commented Feb 14, 2017 at 18:37

4 Answers 4

9

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. how to get name of user_signup session or every session?
2

I have tested this already and struggling along then I realized that I should never use dd() (the dump and die method) after using the session() because your are blocking the system from writing on the session() cookie file.
I'm not really sure about that but it works for me .. let me know if this is True.

1 Comment

Thank you this was my problem, after removing the dd() the session var was finally set.
0

Try this

  session(['user_signup'=> $user_information]);

or

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

and you can check session by logging it

Log::info(Session::get('user_signup'));

check your log file it should be there.

Laravel docs link - https://laravel.com/docs/5.4/session#storing-data

1 Comment

whats the difference between session push and put? will try your solution
0

first : you put something in a session second : check the storage/framework/session folder , if your session work fine you can see your session data in a session folder now.

if you save a session and session folder is still empty :

first change the 'driver' => env('SESSION_DRIVER', 'file') to 'driver' => env('SESSION_DRIVER', 'array') and 'driver' => env('SESSION_DRIVER', 'database')

second set the storage/framework/session permission to 755

and finally go to your kernel file and add bellow code in 'api'

  'api' => [
            
            //add this bellow two line 

            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Session\Middleware\StartSession::class,
          
            'throttle:60,1',
            'bindings',
        ],

then check your session folder again and if you put something in any session you should now see them in this folder, you can delete files in session folder, use the session again to save something in it , going back to session folder and see the session folder is not empty anymore , and you're done, image of the session folder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.