Skip to content
Advertisement

PHP Laravel: How to set or get Session Data?

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 AppHttpControllers;

use IlluminateHttpRequest;
use AppDepartment;
use AppHttpRequests;
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');          
    }

Advertisement

Answer

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');
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement