dongzongpeng6474
2016-10-15 06:07
浏览 71
已采纳

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

图片转代码服务由CSDN问答提供 功能建议

这是我的代码

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

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

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

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

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

它像这样返回,

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

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doukeyong3746487 2016-10-17 10:52
    已采纳

    After adding

    Session::save();
    

    Its working fine

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doudong0425 2016-10-15 06:55

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题