Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ()
{

});
like image 388
UMAIR ALI Avatar asked Dec 10 '22 13:12

UMAIR ALI


2 Answers

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.

enter image description here


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/

like image 113
Mayank Dudakiya Avatar answered Jan 02 '23 19:01

Mayank Dudakiya


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');
like image 41
Alexey Mezenin Avatar answered Jan 02 '23 20:01

Alexey Mezenin