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 ()
{
});
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/
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');
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