Bruno Teixeira
Bruno Teixeira

Reputation: 585

Laravel - Session returns null

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'));

Upvotes: 7

Views: 24241

Answers (4)

Hicham O'Sfh
Hicham O'Sfh

Reputation: 841

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.

Upvotes: 2

arash peymanfar
arash peymanfar

Reputation: 63

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

Upvotes: 0

KuKeC
KuKeC

Reputation: 4620

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

Upvotes: 9

Vikash
Vikash

Reputation: 3561

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

Upvotes: 0

Related Questions