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');
}
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.
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();
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.
To store data, you can use:
Session::put('variableName', $value);
There is also another way, through the global helper:
session(['variableName' => $value]);
To get the variable, you'd use:
Session::get('variableName');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With