Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

majad.shohagh's avatar

How to store session in larvel 5.7

I use laravel 5.6 $value = session('key', 'default'); and I check session()->has('key') it working fine but when I use 5.7 its not working.

0 likes
8 replies
PatrickSJ's avatar
session()->put('key', 'value');
=> null

session()->has('key');
=> true

$value = session('key', 'default');
=> "value"

Are you saying that the last function is returning 'default' instead of 'value'?

hdsavani's avatar

You can use facade also:

Session::has('key')

might be works

hdsavani's avatar

i think you can try to use session facade to put and check both:

\Session::put('key', 'value');
\Session::has('key')

You can try this way

Vilfago's avatar

Could you be more specific that "not working" ?

majad.shohagh's avatar

@VILFAGO - Actually I need store a session for check canvas menu

Laravel Code controller code

//Controller Code
 if ( Session::has('sidebarState')) {
            session::forget('sidebarState');
        } else {
            //colapse sidebar
            session::put('sidebarState', 'sidebar-collapse');
        }


// View Code 

<body class="hold-transition skin-blue sidebar-mini {{ Session::get('sidebarState') }}"></body>

Vilfago's avatar

and ?

You get an error ? Nothing appaears ? Sthing appears, but not what you expect ? What is your session driver ?

if(session()->has('sidebarState')) {
            session()->forget('sidebarState');
        } else {
            //colapse sidebar
            session()->put('sidebarState', 'sidebar-collapse');
        }
}


// View Code 

<body class="hold-transition skin-blue sidebar-mini {{ session('sidebarState') }}"></body>

or closer to the doc, with the $request

if($request->session()->has('sidebarState')) {
           $request->session()->forget('sidebarState');
        } else {
            //colapse sidebar
            $request->session()->put('sidebarState', 'sidebar-collapse');
        }
}

Please or to participate in this conversation.