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'));
Bruno Teixeira
  • 565
  • 4
  • 11
  • 25

4 Answers4

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
muhkuh2005
  • 57
  • 6
KuKeC
  • 4,392
  • 5
  • 31
  • 60
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.

Hicham O-Sfh
  • 731
  • 10
  • 12
  • Thank you this was my problem, after removing the dd() the session var was finally set. – Wak Mar 12 '22 at 03:03
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

Vikash
  • 3,391
  • 2
  • 23
  • 39
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

Ramin eghbalian
  • 2,348
  • 1
  • 16
  • 36