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

__construct(Hasher $hasher, string $model)

Create a new database user provider.

retrieveById(mixed $identifier)

Retrieve a user by their unique identifier.

fetchUserById(mixed $identifier)

Fetch a user by ID from the database.

fetchUserByIdForCache(mixed $identifier)

Fetch a user by ID from the write connection for cache publication.

newUserByIdQuery(mixed $identifier)

Get the query for retrieving a user by ID.

retrieveByToken(mixed $identifier, string $token)

Retrieve a user by their unique identifier and "remember me" token.

void
updateRememberToken(Authenticatable $user, string $token)

Update the "remember me" token for the given user in storage.

retrieveByCredentials(array $credentials)

Retrieve a user by the given credentials.

bool
validateCredentials(Authenticatable $user, array $credentials)

Validate a user against the given credentials.

void
rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)

Rehash the user's password if required and supported.

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.

bool
isCacheEnabled()

Determine if cross-request user caching is enabled.

void
clearUserCache(mixed $identifier)

Clear the cached user for the given identifier.

static void
resolveUserCacheKeyUsing(Closure $callback)

Set the cache key resolver for all cached Eloquent user providers.

static void
resolveUserCacheTagsUsing(Closure $callback)

Set the cache tags resolver for all cached Eloquent user providers.

static void
flushState()

Flush all static state.

string
buildCacheKey(mixed $identifier)

Build the cache key for a user identifier.

static string
resolveCacheKeyIdentifier(mixed $identifier, string $model, Model|Authenticatable|null $user = null)

Resolve the identifier segment of a user cache key.

resolveWriteCache()

Resolve the cache repository to use for puts.

array
effectiveCacheTags()

Compute the effective tag set: static config tags ∪ dynamic resolver output.

void
registerCacheInvalidationEvents()

Register this provider's cache descriptor and set up model event listeners for automatic cache invalidation.

newModelQuery(Model|null $model = null)

Get a new query builder for the model instance.

createModel()

Create a new instance of the model.

getHasher()

Get the hasher implementation.

setHasher(Hasher $hasher)

Set the hasher implementation.

string
getModel()

Get the name of the Eloquent user model.

setModel(string $model)

Set the name of the Eloquent user model.

Closure|null
getQueryCallback()

Get the callback that modifies the query before retrieving users.

withQuery(Closure|null $queryCallback = null)

Set the callback to modify the query before retrieving users.

Details

at line 117
__construct(Hasher $hasher, string $model)

Create a new database user provider.

Parameters

Hasher $hasher
string $model

at line 126
Authenticatable|null retrieveById(mixed $identifier)

Retrieve a user by their unique identifier.

Parameters

mixed $identifier

Return Value

Authenticatable|null

at line 144
protected Authenticatable|null fetchUserById(mixed $identifier)

Fetch a user by ID from the database.

Parameters

mixed $identifier

Return Value

Authenticatable|null

at line 152
protected Authenticatable|null fetchUserByIdForCache(mixed $identifier)

Fetch a user by ID from the write connection for cache publication.

Parameters

mixed $identifier

Return Value

Authenticatable|null

at line 160
protected Builder newUserByIdQuery(mixed $identifier)

Get the query for retrieving a user by ID.

Parameters

mixed $identifier

Return Value

Builder

at line 171
Authenticatable|null retrieveByToken(mixed $identifier, string $token)

Retrieve a user by their unique identifier and "remember me" token.

Parameters

mixed $identifier
string $token

Return Value

Authenticatable|null

at line 195
void updateRememberToken(Authenticatable $user, string $token)

Update the "remember me" token for the given user in storage.

Parameters

Authenticatable $user
string $token

Return Value

void

at line 216
Authenticatable|null retrieveByCredentials(array $credentials)

Retrieve a user by the given credentials.

Parameters

array $credentials

Return Value

Authenticatable|null

at line 249
bool validateCredentials(Authenticatable $user, array $credentials)

Validate a user against the given credentials.

Parameters

Authenticatable $user
array $credentials

Return Value

bool

at line 267
void rehashPasswordIfRequired(Authenticatable $user, array $credentials, bool $force = false)

Rehash the user's password if required and supported.

Parameters

Authenticatable $user
array $credentials
bool $force

Return Value

void

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.

Parameters

string|null $storeName
int $ttl
string|null $prefix
array|null $tags

optional tag names enabling tag-based bulk flush; requires any-mode tag support

Return Value

EloquentUserProvider

Exceptions

InvalidArgumentException

at line 346
bool isCacheEnabled()

Determine if cross-request user caching is enabled.

Return Value

bool

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.

Parameters

mixed $identifier

Return Value

void

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.

Parameters

Closure $callback

Return Value

void

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.

Parameters

Closure $callback

Return Value

void

at line 420
static void flushState()

Flush all static state.

Return Value

void

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.

Parameters

mixed $identifier

Return Value

string

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.

Parameters

mixed $identifier
string $model
Model|Authenticatable|null $user

Return Value

string

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).

Return Value

Repository

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.

Return Value

array

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.

Return Value

void

at line 598
protected Builder newModelQuery(Model|null $model = null)

Get a new query builder for the model instance.

Parameters

Model|null $model

Return Value

Builder

at line 614
Model createModel()

Create a new instance of the model.

Return Value

Model

at line 624
Hasher getHasher()

Get the hasher implementation.

Return Value

Hasher

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.

Parameters

Hasher $hasher

Return Value

EloquentUserProvider

at line 647
string getModel()

Get the name of the Eloquent user model.

Return Value

string

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.

Parameters

string $model

Return Value

EloquentUserProvider

at line 676
Closure|null getQueryCallback()

Get the callback that modifies the query before retrieving users.

Return Value

Closure|null

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.

Parameters

Closure|null $queryCallback

Return Value

EloquentUserProvider