简体   繁体   中英

How to get session values from database in laravel

Here is my code

class CreateSessionsTable extends Migration
{
public function up()
    {
        Schema::create('sessions', function (Blueprint $table) {
            $table->string('id')->unique();
            $table->text('payload');
            $table->integer('last_activity');
        });
    }
    public function down()
    {
        Schema::drop('sessions');
    }
}

In config/session.php file changed 'driver' => env('SESSION_DRIVER', 'database'), In DB table was created. When user login i used below code to push values into session

Session::put('userid', $userid);
Session::put('useremail', $useremail);

Then another method i checked session

$data = Session::all();
        var_dump($data);

Its returning like this,

array (size=3)
  '_token' => string 'kD3zC2YF4v63RF1EkAVe0YoM4zLj9kGtiV4vpEzG' (length=40)
  '_previous' => 
    array (size=1)
      'url' => string 'http://localhost/Login' (length=28)
  'flash' => 
    array (size=2)
      'old' => 
...

So how to get that user values from db.

After adding

Session::save();

Its working fine

Which Laravel version are you using? If it's 5.3, try with the following:

//Store value
$request->session()->put('key', 'value');

//Get value
$value = $request->session()->get('key', 'valueIfItsNotReturned');

The second argument is the default value that will be returned if the key does not exist in the session.

If it doesn't work, see if you get the same problem by changing the session driver to file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM