saravanan mp
saravanan mp

Reputation: 783

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.

Upvotes: 0

Views: 2759

Answers (2)

saravanan mp
saravanan mp

Reputation: 783

After adding

Session::save();

Its working fine

Upvotes: 1

Moris
Moris

Reputation: 3083

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.

Upvotes: 1

Related Questions