Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Fluber's avatar

Get current sessions for user Laravel

Hello. I want do table with active sessions for user. Where I want show to them:

  • Device (Mac, Phone, Tablet.. other)
  • Browser(Chrome, Safari, Opera .. etc)
  • Last Activity
  • Date, when session will be closed
  • IP
  • Button for terminate certain session and button for terminate all sessions.

How I can do it? May be someone know package for this functions?

0 likes
3 replies
Fluber's avatar

@REALRANDYALLEN - Thanks. But How I can get Date, when session will be closed? In database not exists column for this.

realrandyallen's avatar

@DRONAX - You could create a Session model, that way you can setup an Eloquent relationship to your User model...the whole thing could look like this

Session model

<?php

namespace App;

use Illuminate\Support\Carbon;
use Illuminate\Database\Eloquent\Model;

class Session extends Model
{
    protected $appends = ['expires_at'];

    public function isExpired()
    {
        return $this->last_activity < Carbon::now()->subMinutes(config('session.lifetime'))->getTimestamp();
    }

    public function getExpiresAtAttribute()
    {
        return Carbon::createFromTimestamp($this->last_activity)->addMinutes(config('session.lifetime'))->toDateTimeString();
    }
}

User model

...

use App\Session;

...

class User extends Authenticatable
{
    ...
    protected $with = ['sessions'];

    public function sessions()
    {
        return $this->hasMany(Session::class);
    }
    ...

Usage

$sessions = auth()->user()->sessions;

$expired = $sessions->first()->isExpired();
$expires_at = $sessions->first()->expires_at;
4 likes

Please or to participate in this conversation.