EloquentUserProvider
class EloquentUserProvider implements UserProvider
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, modelSegment: 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 | $queryCallback | The callback that may modify the user retrieval queries. |
|
| protected Repository|null | $cache | The cache store for user lookups. |
|
| 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. |
|
| protected string | $modelSegment | Memoized model key segment (the fully qualified class name). |
Methods
Retrieve a user by their unique identifier.
Fetch a user by ID from the database.
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.
Ensure the resolved cache store supports tags in any-mode.
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 116
__construct(Hasher $hasher, string $model)
Create a new database user provider.
at line 125
Authenticatable|null
retrieveById(mixed $identifier)
Retrieve a user by their unique identifier.
at line 141
protected Authenticatable|null
fetchUserById(mixed $identifier)
Fetch a user by ID from the database.
at line 153
Authenticatable|null
retrieveByToken(mixed $identifier, string $token)
Retrieve a user by their unique identifier and "remember me" token.
at line 177
void
updateRememberToken(Authenticatable $user, string $token)
Update the "remember me" token for the given user in storage.
at line 196
Authenticatable|null
retrieveByCredentials(array $credentials)
Retrieve a user by the given credentials.
at line 229
bool
validateCredentials(Authenticatable $user, array $credentials)
Validate a user against the given credentials.
at line 247
void
rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)
Rehash the user's password if required and supported.
at line 280
EloquentUserProvider
enableCache(string|null $storeName, int $ttl = 300, string|null $prefix = 'auth_users', 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_users') 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 in its prior uncached state 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 315
bool
isCacheEnabled()
Determine if cross-request user caching is enabled.
at line 326
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 351
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 372
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 380
static void
flushState()
Flush all static state.
at line 401
protected void
ensureTaggableAnyModeStore(Repository $cache)
Ensure the resolved cache store supports tags in any-mode.
Auth caching only supports tag-based bulk flush via any-mode because:
- any-mode keys are independent of tags, so reads and per-user forgets stay on the plain repo (no mode branching in the hot path);
- stack caches can layer short-lived local reads over shared any-mode tag indexes without changing auth's plain-key read path;
- the dynamic tag resolver can't cause cross-context invalidation bugs since the auto-invalidation listener never touches tags.
at line 431
protected string
buildCacheKey(mixed $identifier)
Build the cache key for a user identifier.
Always includes the fully qualified model class name (memoized in enableCache()) 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 446
static protected string
resolveCacheKeyIdentifier(mixed $identifier, string $model, Model|Authenticatable|null $user = null)
Resolve the identifier segment of a user cache key.
at line 469
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 492
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 523
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, modelSegment) triple is stored under a deterministic hash so duplicate configs collapse. On save/delete, the listener iterates descriptors for the model class, re-resolves each store by name via the cache manager, rebuilds the key using the current global resolver callback, and calls forget(). 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 574
protected Builder
newModelQuery(Model|null $model = null)
Get a new query builder for the model instance.
at line 590
Model
createModel()
Create a new instance of the model.
at line 600
Hasher
getHasher()
Get the hasher implementation.
at line 611
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 623
string
getModel()
Get the name of the Eloquent user model.
at line 636
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 648
Closure|null
getQueryCallback()
Get the callback that modifies the query before retrieving users.
at line 661
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.