ryancco/laravel-sessions

The missing model for Laravel sessions

0.1 2020-04-26 05:23 UTC

README

Packagist GitHub Workflow Status Packagist

The missing model for Laravel sessions with some conveniently opinionated functionality built-in.

Installation

Laravel Sessions can be installed via Composer.

composer require ryancco/laravel-sessions

Usage

Apply the HasSessions trait to your User model

class User extends Authenticatable
{
    use \Ryancco\Sessions\HasSessions;

Get a user's active sessions

@method active(Carbon|int $last_activity = 60)

$user->sessions()
    ->active(30) // active in the last 30 minutes
    ->get();

Get a user's inactive sessions

@method inactive(Carbon|int $last_activity = 60)

$user->sessions()->inactive(
    now()->subHours(2) // not active in the last 2 hours
)->get();

Get a session's device

Currently, this package leverages jenssegers/agent for device information. Check out the pieces used and head over to that repository for any further information.

$device = $user->sessions()
    ->latest('last_activity')
    ->first()
    ->device();

$device->device();
$device->browser();
$device->platform();
$device->browserVersion();
$device->platformVersion();

Preqrequisites

Laravel Sessions expects you to be using Laravel's database session driver. To learn more about this and other session driver preqrequisites, check out the documentation.

Contributing

Please report any problems by creating an issue. Pull requests are always welcomed.