Laravel - 会话返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42233343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:19:41  来源:igfitidea点击:

Laravel - Session returns null

phplaravelsession

提问by Bruno Teixeira

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?

我第一次在 Laravel 中使用 session 并且我正在尝试做一个多步骤的表单,所以我认为使用 session 将是一个聪明的举动。但是下面的代码返回一个空值,我做错了什么?

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

回答by KuKeC

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

回答by Vikash

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

Laravel 文档链接 - https://laravel.com/docs/5.4/session#storing-data

回答by arash peymanfar

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

首先将 'driver' => env('SESSION_DRIVER', 'file') 更改为 'driver' => env('SESSION_DRIVER', 'array') 和 'driver' => env('SESSION_DRIVER', 'database')

second set the storage/framework/session permission to 755

第二个将存储/框架/会话权限设置为 755

and finaly go to your kernal file and add bellow code in 'api'

最后转到您的内核文件并在“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

然后再次检查您的会话文件夹,如果您在任何会话中放置了某些内容,您现在应该在此文件夹中看到它们,您可以删除会话文件夹中的文件,再次使用会话将某些内容保存在其中,返回会话文件夹并查看会话文件夹不再是空的,大功告成,会话文件夹的图像