Open the file config/session.php
and change the driver
to database
.
We need to create the sessions
table, so use the following artisan command php artisan session:table
to generate the migration file.
On this newly generated migration, you need to add a new user_id
column, this is so we can relate the session to a user, if that user is logged in of course.
Open the file migrations/xxxx_xx_xx_xxxxxx_create_session_table.php
and add the following inside the Schema::create
:
$t->integer('user_id')->nullable();
Here is how the full migration should look:
<?php
use Illuminate\Database\Migrations\Migration;
class CreateSessionTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sessions', function($t)
{
$t->string('id')->unique();
$t->text('payload');
$t->integer('last_activity');
$t->integer('user_id')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('sessions');
}
}
Run composer dump-autoload
and php artisan migrate
.
Note: If you don't have Composer installed globally, just use php composer.phar dump-autoload
.
Save the Eloquent Model somewhere on your application as Session.php
.
Note: The recommended place to save this is on the app
directory.
Now you just need to know how to use it.