'Session not working in laravel 5.3
I am trying to create session like below code but session not working please sussgest me any soluton.
Save
$data = array(
"id" => $row->id,
"name" => $row->name
);
Session($data);
Fetch
Session('id');
I also tried web middleware but till the same session not working
Route::group(['middleware' => ['web']], function ()
{
});
Solution 1:[1]
I have recently solved this issue.
If you are trying to save array in session or if you want to push data into the session this try following.
Solution:
First go to Kernel.php into the "App\Http\Kernel.php" and add this line \Illuminate\Session\Middleware\StartSession::class, into the $middleware array. This will start storing the data.
For session array
Session::push('cart', $product);
For Single value
Replace session_key with the variable you want.
Session::put('session_key');
Reference: https://www.scratchcode.io/session-not-working-in-laravel/
Solution 2:[2]
You should remove web middleware from routes to fix the problem with sessions.
Also correct syntax for persisting data is:
session(['key' => $data]);
To get data use:
session('key');
Solution 3:[3]
Session will be working fine if the following step can be followed...
First Step:
Add the following code inside a controller(where Session will be used to save data)
use Session;
Second Step:
Inside a method of that controller, Session code like below:
Session::put('name', 'Sabuz');
Session::put('data', $data);
any data can be saved but make sure first parameter of put method is key and second is its value
Third Step:
That data can be viewed from anywhere with the below command as long as session caries that data
$name = Session::get('name'); //get method just use the key of a put method
echo $name;
Hopefully, it will be workable.
Solution 4:[4]
If you want persistence sessions, use session()->save() or Session::save()
$data = array("id" => $row->id, "name" => $row->name);
session($data);
//or
session()->put('key', 'value');
session()->save();
echo session('id');
Also, the 'storage' directory should have write permission.
chmod -R a+rw storage/
Solution 5:[5]
My session is not working because I tried to put and fetch in controller constructor and Laravel 5.3 not supporting directly put and fetch session in a constructor. If you want to put and fetch session in a constructor you need to add below code in a constructor.
function __construct()
{
$this->middleware(function ($request, $next)
{
});
}
Solution 6:[6]
I had the problem of writing to sessions 1st time per session working, but updates not working.
The fix was the old classic...
use Session;
Solution 7:[7]
Save the data
session()->put('data' => $data);
Get the data
session()->get('data');
Solution 8:[8]
Use session like
use Illuminate\Support\Facades\Session;
Set Session:
Session::flash('key', 'Value');
View File :
@if(Session::has('key'))
<div class="alert-success">
{{ Session::get('key') }}
</div>
@endif
Reference: https://laravel.com/docs/5.3/facades#facade-class-reference https://laravel.com/api/5.3/Illuminate/Support/Facades/Session.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Community |
| Solution 3 | sabuz |
| Solution 4 | |
| Solution 5 | UMAIR ALI |
| Solution 6 | AdamJones |
| Solution 7 | MrChux |
| Solution 8 | Md. Abu Taleb |

