EloquentUserProvider
class EloquentUserProvider implements UserProvider
Constants
| DEFAULT_CACHE_TTL |
|
| DEFAULT_CACHE_PREFIX |
|
Properties
| static protected null|Authenticatable>, null|Authenticatable)): string | $cacheKeyResolver | The callback used to build the identifier segment of cache keys. |
|
| static protected null|Closure(): list<string> | $cacheTagsResolver | Global resolver returning additional per-request tags to union with the static config tags at every cache write. |
|
| static protected array<class-string, array<string, array{storeName: ?string, prefix: string}>> | $cachedProviders | Registry of cache descriptors per model class. |
|
| static protected array<class-string, true> | $cacheEventsRegistered | Whether model event listeners have been registered for a model class. |
|
| protected null|Builder<mixed>): mixed | $queryCallback | The callback that may modify the user retrieval queries. |
|
| protected Repository|null | $cache | The cache store for user lookups. |
|
| protected ModelCacheCoordinator | $cacheCoordinator | The coordinator for shared user cache entries. |
|
| protected string|null | $cacheStoreName | The cache store name (null = default store). |
|
| protected int | $cacheTtl | The cache TTL in seconds. |
|
| protected string | $cachePrefix | The cache key prefix. |
|
| protected null|list<string> | $cacheTags | Static tags applied to every cache write (unioned with whatever the tag resolver returns). Null = no tags configured. |
Methods
Retrieve a user by their unique identifier.
Fetch a user by ID from the database.
Fetch a user by ID from the write connection for cache publication.
Get the query for retrieving a user by ID.
Retrieve a user by their unique identifier and "remember me" token.
Update the "remember me" token for the given user in storage.
Retrieve a user by the given credentials.
Validate a user against the given credentials.
Rehash the user's password if required and supported.
Enable cross-request caching for user lookups.
Determine if cross-request user caching is enabled.
Clear the cached user for the given identifier.
Set the cache key resolver for all cached Eloquent user providers.
Set the cache tags resolver for all cached Eloquent user providers.
Flush all static state.
Build the cache key for a user identifier.
Resolve the identifier segment of a user cache key.
Resolve the cache repository to use for puts.
Compute the effective tag set: static config tags ∪ dynamic resolver output.
Register this provider's cache descriptor and set up model event listeners for automatic cache invalidation.
Create a new instance of the model.
Get the name of the Eloquent user model.
Set the name of the Eloquent user model.
Get the callback that modifies the query before retrieving users.
Details
at line 117
__construct(Hasher $hasher, string $model)
Create a new database user provider.
at line 126
Authenticatable|null
retrieveById(mixed $identifier)
Retrieve a user by their unique identifier.
at line 144
protected Authenticatable|null
fetchUserById(mixed $identifier)
Fetch a user by ID from the database.
at line 152
protected Authenticatable|null
fetchUserByIdForCache(mixed $identifier)
Fetch a user by ID from the write connection for cache publication.
at line 160
protected Builder
newUserByIdQuery(mixed $identifier)
Get the query for retrieving a user by ID.
at line 171
Authenticatable|null
retrieveByToken(mixed $identifier, string $token)
Retrieve a user by their unique identifier and "remember me" token.
at line 195
void
updateRememberToken(Authenticatable $user, string $token)
Update the "remember me" token for the given user in storage.
at line 216
Authenticatable|null
retrieveByCredentials(array $credentials)
Retrieve a user by the given credentials.
at line 249
bool
validateCredentials(Authenticatable $user, array $credentials)
Validate a user against the given credentials.
at line 267
void
rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)
Rehash the user's password if required and supported.
at line 300
EloquentUserProvider
enableCache(string|null $storeName, int $ttl = self::DEFAULT_CACHE_TTL, string|null $prefix = self::DEFAULT_CACHE_PREFIX, array|null $tags = null)
Enable cross-request caching for user lookups.
Accepts a store name (or null for the default store) rather than a pre-resolved repository so the descriptor registry can re-resolve by name on invalidation and avoid holding strong references.
A null or empty-string prefix is normalized to the feature default ('auth_user') so misconfiguration does not create hard-to-read keys with a leading colon.
The store is validated before any instance state is mutated, so a rejected store leaves the provider's existing cache configuration unchanged and does not register a descriptor or model event listeners.
Boot-only. User providers are held by cached guards; runtime use mutates the provider used by every subsequent authentication lookup.
at line 346
bool
isCacheEnabled()
Determine if cross-request user caching is enabled.
at line 357
void
clearUserCache(mixed $identifier)
Clear the cached user for the given identifier.
Uses the same key resolver as retrieveById(), passing null for the user model so context-aware keys use the caller's current context.
at line 391
static void
resolveUserCacheKeyUsing(Closure $callback)
Set the cache key resolver for all cached Eloquent user providers.
The callback receives the user identifier and provider model class. Invalidation triggered by a model event also provides the saved or deleted user; lookups and manual invalidation provide null. It should return a string that uniquely identifies the user within the current context (e.g., including tenant ID for multi-tenant apps). Called once in a service provider's boot() method — the closure is evaluated fresh on each lookup or invalidation so per-request context like tenant ID is current.
The fully qualified model class name is always included in the key automatically. The resolver only controls the identifier segment.
Boot-only. The resolver persists in a static property for the worker lifetime and runs on every cached user lookup and invalidation.
at line 412
static void
resolveUserCacheTagsUsing(Closure $callback)
Set the cache tags resolver for all cached Eloquent user providers.
The callback receives no arguments and should return a list of tag names for the current request context. Called fresh on each cache put so it can read per-request state.
Effective tags applied to each write = static config tags (per-provider, from auth.providers.*.cache.tags) unioned with the resolver's return value.
Boot-only. The resolver persists in a static property for the worker lifetime and runs on every cached user write.
at line 420
static void
flushState()
Flush all static state.
at line 436
protected string
buildCacheKey(mixed $identifier)
Build the cache key for a user identifier.
Always includes the fully qualified model class name so providers using different models never collide, even when two models share a basename across namespaces. The custom resolver (if set) controls the identifier segment only.
at line 451
static protected string
resolveCacheKeyIdentifier(mixed $identifier, string $model, Model|Authenticatable|null $user = null)
Resolve the identifier segment of a user cache key.
at line 474
protected Repository
resolveWriteCache()
Resolve the cache repository to use for puts.
If static tags are configured (opt-in gate), returns a tagged repository with the union of static and dynamic tags. Otherwise returns the plain repo. Computed per-write because dynamic tags can change per request.
Uses Repository::tags() rather than reaching into the raw store via getStore()->tags() so the tagged cache inherits the repository's config and event dispatcher wiring (CachePut events etc. fire correctly on tagged writes).
at line 497
protected array
effectiveCacheTags()
Compute the effective tag set: static config tags ∪ dynamic resolver output.
The static tag config is the feature gate: if no static tags are configured, returns an empty array and the dynamic resolver is ignored. This matches where enableCache() performs the store validation — without static tags, the store was never checked for TaggableStore + any-mode support, so there's no safe way to apply dynamic tags either.
at line 528
protected void
registerCacheInvalidationEvents()
Register this provider's cache descriptor and set up model event listeners for automatic cache invalidation.
Uses a descriptor-based registry: each (storeName, prefix) pair is stored under a serialized identity for its model class so duplicate configs collapse. On save/delete, the listener resolves the cache-key identifier while the model context is available. After commit, it reads the current descriptors, re-resolves each store by name, and invalidates the exact key under its fill lock. Nothing holds a reference to a provider instance — safe against forgetGuards() + re-resolve cycles under Swoole.
Event listener registration is guarded by the model's dispatcher being non-null — HasEvents::registerModelEvent() silently no-ops when the dispatcher isn't set, so we only mark the class as registered after a successful attempt, leaving a retry window on the next enableCache() call.
at line 598
protected Builder
newModelQuery(Model|null $model = null)
Get a new query builder for the model instance.
at line 614
Model
createModel()
Create a new instance of the model.
at line 624
Hasher
getHasher()
Get the hasher implementation.
at line 635
EloquentUserProvider
setHasher(Hasher $hasher)
Set the hasher implementation.
Boot or tests only. User providers are held by cached guards; runtime use mutates password verification for every subsequent authentication lookup.
at line 647
string
getModel()
Get the name of the Eloquent user model.
at line 660
EloquentUserProvider
setModel(string $model)
Set the name of the Eloquent user model.
Boot or tests only. User providers are held by cached guards; runtime use changes the model used by every subsequent authentication lookup.
at line 676
Closure|null
getQueryCallback()
Get the callback that modifies the query before retrieving users.
at line 689
EloquentUserProvider
withQuery(Closure|null $queryCallback = null)
Set the callback to modify the query before retrieving users.
Boot or tests only. User providers are held by cached guards; runtime use mutates the query applied to every subsequent authentication lookup.