会议不在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 ()
{

});

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');

Save the data

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

Get the data

session()->get('data');

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

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

   });
}

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.

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 Kernal.php into the "app\Http\Kernal.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');