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

tinkerwell logo
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article