Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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');          
    }
like image 486
User57 Avatar asked May 18 '16 06:05

User57


People also ask

How can we access session in laravel?

To access the session data, we need an instance of session which can be accessed via HTTP request. After getting the instance, we can use the get() method, which will take one argument, “key”, to get the session data.

How can I see session variables in laravel?

If you just want to see contents of session, try dd() : dd(session()->all()); If not, just use this to get all info: $data = session()->all();

Where is session stored in laravel?

Laravel ships with several great drivers out of the box: file - sessions will be stored in storage/framework/sessions . cookie - sessions will be stored in secure, encrypted cookies. database - sessions will be stored in a database used by your application.


1 Answers

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');
like image 51
Tommy Wu Avatar answered Sep 16 '22 23:09

Tommy Wu