8

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

});
UMAIR ALI
  • 1,055
  • 5
  • 14
  • 25

8 Answers8

13

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/

Mayank Dudakiya
  • 3,635
  • 35
  • 35
  • 1
    Yes! Adding the `StartSession` middleware to the list of "global" middlewares in Kernel.php is what solved it for me ... – leo Apr 27 '22 at 19:05
4

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');
Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
4

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.

sabuz
  • 829
  • 8
  • 10
2

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/
namal
  • 1,164
  • 1
  • 10
  • 15
0

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

   });
}
UMAIR ALI
  • 1,055
  • 5
  • 14
  • 25
0

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;
AdamJones
  • 652
  • 1
  • 15
  • 38
-1

Save the data

session()->put('data' => $data);

Get the data

session()->get('data');
MrChux
  • 110
  • 1
  • 10
-1

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

Md. Abutaleb
  • 1,590
  • 1
  • 14
  • 24