如何从laravel中的数据库获取会话值

如何从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.

这是我的代码 p>

 类CreateSessionsTable扩展了Migration 
  {
public function up()
 {
 Schema :: create('sessions',function(Blueprint $ table){
 $ table-> string('id') - > unique(); 
  $ table-> text('payload'); 
 $ table-> integer('last_activity'); 
}); 
} 
公共函数down()
 
 
 \ {
 
 \  ('sessions'); 
} 
} 
  code>  pre> 
 
 

config / session.php code>文件中更改'driver' => env('SESSION_DRIVER','数据库'), code> 在数据库表中创建了。 当用户登录时,我使用下面的代码将值推送到会话 p>

  Session :: put('userid',$ userid); 
Session :: put('useremail',  $ useremail); 
  code>  pre> 
 
 

然后我检查会话的另一种方法 p>

  $ data = Session :: all()  ; 
 var_dump($ data); 
  code>  pre> 
 
 

它像这样返回, p>

  array(size = 3  )
'_token'=>  string'kD3zC2YF4v63RF1EkAVe0YoM4zLj9kGtiV4vpEzG'(长度= 40)
'_previous'=>  
 array(size = 1)
'url'=> 字符串'http:// localhost / Login'(长度= 28)
'flash'=>  
 array(size = 2)
'old'=>  
 ... 
  code>  pre> 
 
 

那么如何从db获取该用户值。 p> div>

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.