Laravel Packages

Playa: Cookie-Based Temporary Players for Laravel

Published
Playa: Cookie-Based Temporary Players for Laravel image

Playa gives a Laravel app a lightweight Player model for visitors who aren't logged in. It's aimed at QR-code journeys, event games, voting screens, kiosk flows, and demos where you need to recognize a returning device without putting users through registration.

Tracking Players via Middleware

Add the playa middleware to any route where you want a player resolved or created:

Route::get('/join/{game}', JoinGameController::class)
->middleware(['web', 'playa'])
->name('games.join');

On the first visit the middleware creates a player record and sets the cookie. On subsequent requests it resolves the existing player and, by default, refreshes the expiry timestamp.

Accessing the Current Player

Playa exposes the resolved player through both the request and a facade:

$player = $request->player();
 
use CharlieLangridge\Playa\Facades\Playa;
 
$player = Playa::player();
$player = Playa::findByUuid($uuid);
Playa::forget();

The model includes name and username columns plus a JSON data column for any application-specific details you want to attach — score, answers, position in a flow, and so on.

Linking to Authenticated Users

A player can be associated with a real user account without forcing the visitor to log in first. This is useful when someone starts a flow anonymously and later authenticates:

$player->linkUser(auth()->user());
$player->unlinkUser();

The package supports bigint, uuid, ulid, and string user key types via the user_id_type config option, and auto_link_authenticated_user will attach the current user automatically when set to true.

Lifecycle Events

Playa dispatches events at each stage so you can hook into player activity without modifying the middleware:

  • PlayerCreated
  • PlayerResolved
  • PlayerRenewed
  • PlayerExpired
  • PlayerLinkedToUser

These are handy for analytics, audit trails, or triggering follow-up jobs when a device returns.

Expiry and Pruning

Players have a configurable lifetime (default 30 days) and can renew on each visit. Expired records aren't deleted automatically — run the artisan command to clean them up, optionally with a grace period:

php artisan playa:prune
php artisan playa:prune --hours=24

Playa requires PHP 8.4 and supports Laravel 11, 12, and 13.

To learn more, view the source on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
Agent Run Observability in Laravel AI SDK 0.11 image

Agent Run Observability in Laravel AI SDK 0.11

Read article
Debounced Queued Event Listeners in Laravel image

Debounced Queued Event Listeners in Laravel

Read article
Statamic Mailables Viewer Previews Laravel Emails in the Control Panel image

Statamic Mailables Viewer Previews Laravel Emails in the Control Panel

Read article
Laravel Tackle: Run an AI Coding Agent in Your Laravel App image

Laravel Tackle: Run an AI Coding Agent in Your Laravel App

Read article
Queue::forward(): Reroute Laravel Queues in One Place image

Queue::forward(): Reroute Laravel Queues in One Place

Read article
Laravel Read-Through Filesystem: Lazy Storage Migration image

Laravel Read-Through Filesystem: Lazy Storage Migration

Read article