@bobzamin You’re using the session completely wrong. Just store whatever data you need in the session itself.
How to add extra column to Session table and how to store data in it?
Hi. I'm trying to add an extra field to the sessions table so that along with the existing fields like user_id, user_agent, ip_address etc., the extra office_id column gets records stored in it on login.
I have: Changed the session driver both in config/sessions and .env files
config/session.php
'driver' => env('SESSION_DRIVER', 'app.database'),
Added office_id to the sessions migration file
Schema::create('sessions', function (Blueprint $table) {
$table->string('id')->primary();
$table->foreignId('user_id')->nullable()->index();
$table->foreignId('office_id')->nullable()->index();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity')->index();
$table->timestamps();
});
Ran
php artisan migrate:fresh --seed
Extended the DatabaseSessionHandler
<?php
namespace App\Session;
class DatabaseSessionHandler extends \Illuminate\Session\DatabaseSessionHandler
{
/**
* {@inheritDoc}
*/
protected function officeId()
{
return $this->container->make('request')->header('office');
}
public function write($sessionId, $data)
{
$user_id = (auth()->check()) ? auth()->user()->id : null;
$office_id = $this->officeId();
if ($this->exists) {
$this->getQuery()->where('id', $sessionId)->update([
'payload' => base64_encode($data), 'last_activity' => time(), 'user_id' => $user_id, 'office_id' => $office_id,
]);
} else {
$this->getQuery()->insert([
'id' => $sessionId, 'payload' => base64_encode($data), 'last_activity' => time(), 'user_id' => $user_id, 'office_id' => $office_id,
]);
}
$this->exists = true;
}
}
Wrote a SessionServiceProvider and registered it in config/app.php in Provider array
App/Providers/SessionServiceProvider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Session\DatabaseSessionHandler;
class SessionServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->session->extend('app.database', function ($app) {
$lifetime = $this->app->config->get('session.lifetime');
$table = $this->app->config->get('session.table');
$connection = $app->app->db->connection($this->app->config->get('session.connection'));
return new DatabaseSessionHandler($connection, $table, $lifetime, $this->app);
});
}
}
config/app.php
App\Providers\SessionServiceProvider::class,
Wrote the logic to get the Office name at login (the user picks the office from dropdown on login screen)
protected function attemptLogin(Request $request)
{
$office = Office::where('name', $request->office)->first();
if($office && $office->is_active === 1) {
return $this->guard()->attempt(
['username' => $request->username, 'password' => $request->password, 'is_active' => 1], $request->filled('remember')
);
}
}
But when the user logs in, the database gets filled with records for all other fields except the office_id. What is missing? Am I doing it wrong? Is there an easier way of doing this? Do I have to create a model for the Session to be able to store the office_id to the sessions table? How do I get the user request information to the DatabaseSessionHandler to extract office name from it?
Please help figure this out. Thank you in advance.
Please or to participate in this conversation.