17

I want to store some data in session for some testing purpose. I have written the session code also in controller. But in console.log ->resources -> session I couldn't find any value which I stored. If anyone help me to find the mistake which I have done in my controller please.

Here is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Department;
use App\Http\Requests;
use Cookie;
use Tracker;
use Session;

 public function postdepartmentSave(Request $request)
    {
         $this->validate($request,[
            'code' => 'required|min:2|max:7|unique:departments',          
            'name' => 'required|unique:departments',          
            ]);
            $department = new Department();           
            $department->code = $request->Input(['code']);            
            $department->name = $request->Input(['name']);
              $name=   $department->name;
         Session::put('name', $name);
        dd($name);
            $department->save();            
            return redirect('departmentSavePage');          
    }

3 Answers 3

54

Storing data

To store data, you can use:

Session::put('variableName', $value);

There is also another way, through the global helper:

session(['variableName' => $value]);

Getting data

To get the variable, you'd use:

Session::get('variableName');
Sign up to request clarification or add additional context in comments.

8 Comments

Why nothing is shown in Resources->Session Storage when I seen in console?
Have you tried to get in other place ? Like in view or controller. To see if it is returning the value or not. If not, then you confirm if $name is null or not
$name is not null because I have seen it using dd();
Try to Session::get('variableName'); in other place. For example in the view, to see if it is returning the value or not.
Nope, I just write {{$name}} to access the session data but it's showing Undefined variable: name
|
15

You haven't made a coding mistake. The issue is that you're getting a little confused between server side sessions and HTML5's sessionStorage.

The Resources->Session Storage view on the Chrome Developer Tools is only showing you the information stored in the HTML5 sessionStorage object. This is client side session information that is accessed via client side javascript, and is completely separate from any type of server side session information.

For example, run sessionStorage.setItem('key', 'value'); in your javascript console, and you will see that data show up in the Resources->Session Storage view.

Your PHP/Laravel code is dealing with server side sessions. This information is not going to show up in any type of Developer Tools view because it is not meant to be accessed by client side tools. By default, Laravel uses file based session storage, which means your session information is stored in files on your server (in storage/framework/sessions). Even if you change the session storage to use cookies, the session information will be encrypted so that the client side cannot read the data. This is for security purposes.

You can read more about Laravel sessions here.
You can read more about HTML5 sessionStorage here.

Comments

3

if you have existing value on that session variable, first you need to clear it. then you can replace new value like this.

 session()->forget('key');
 session()->put('key', 'value');

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.