I'm working with the Session table in Laravel and I just don't see how these records delete themselves after some time.
I haven't found any comprehensive guide on how the Session table works.
Here's my code so far:
SessionHandler.php
<?php
namespace AppExtensions;
class SessionHandler implements SessionHandlerInterface
{
public function open($savePath, $sessionName) {}
public function close() {}
public function read($sessionId) {}
public function write($sessionId, $data) {}
public function destroy($sessionId) {}
public function gc($lifetime) {}
}
AppSession.php
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use CarbonCarbon;
class Session extends Model
{
protected $hidden = ['payload'];
/**
* The database table used by the model.
*
* @var string
*/
public $table = 'sessions';
public $timestamps = false;
/**
* Returns the user that belongs to this entry.
*/
public function user()
{
return $this->belongsTo('User');
}
/**
* Returns all the guest users.
*
* @param $query
* @return IlluminateDatabaseEloquentBuilder
*/
public function scopeGuests($query)
{
return $query->whereNull('user_id')->where('last_activity', '>=', strtotime(Carbon::now()->subMinutes(25)));
}
/**
* Returns all the registered users.
*
* @param $query
* @return IlluminateDatabaseEloquentBuilder
*/
public function scopeRegistered($query)
{
return $query->whereNotNull('user_id')->where('last_activity', '>=', strtotime(Carbon::now()->subMinutes(25)))->with('user');
}
/**
* Updates the session of the current user.
*
* @param $query
* @return IlluminateDatabaseEloquentBuilder
*/
public function scopeUpdateCurrent($query)
{
return $query->where('id', Session::getId())->update([
'user_id' => ! empty(Auth::user()) ? Auth::id() : null
]);
}
}
AppServiceProvider.php
Session::extend('handler', function ($app) {
// Return implementation of SessionHandler
return new SessionHandler;
});
.env
SESSION_LIFETIME = 1
session.php
'lifetime' => env('SESSION_LIFETIME', 1),
'expire_on_close' => false,
What I'm basically trying to do is show each Guests/Users activity, but once their session/time limit is over -- POP! It deletes from the database too. Can anyone please share some insight on how to achieve this?