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
添加
playa
middleware to any route where you want a player resolved or created:
路线
::
得到
(
'/join/{game}'
,
JoinGameController
::班级
)
->
中间件
([
'网络'
,
'playa'
])
->
姓名
(
'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
=
$请求
->
player
();使用
CharlieLangridge\Playa\Facades\Playa
;$player
=
Playa
::
player
();$player
=
Playa
::
findByUuid
($uuid);Playa
::
忘记
();
The model includes
name
和
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
(
授權
()
->
用户
());$player
->
unlinkUser
();
The package supports
bigint
,
uuid
,
ulid
, 和
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:
PlayerCreatedPlayerResolvedPlayerRenewedPlayerExpiredPlayerLinkedToUser
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
工匠
playa:prunephp
工匠
playa:prune
--hours=24
Playa requires PHP 8.4 and supports Laravel 11, 12, and 13.
To learn more, view the source on GitHub 。







