class User extends AbstractModel

Traits

Properties

bool $timestamps Indicates if the model should be timestamped. Turn off by default. from AbstractModel
protected callable[] $afterSaveCallbacks An array of callbacks to be run once after the model is saved. from AbstractModel
protected callable[] $afterDeleteCallbacks An array of callbacks to be run once after the model is deleted. from AbstractModel
static $customRelations from AbstractModel
static $dateAttributes from AbstractModel
static $defaults from AbstractModel
protected array $pendingEvents from EventGeneratorTrait
static protected $visibilityScopers from ScopeVisibilityTrait
protected array $dates The attributes that should be mutated to dates.
protected string[]|null $permissions An array of permissions that this user has.
protected Session $session
static protected $groupProcessors An array of callables, through each of which the user's list of groups is passed before being returned.
array $preferences
static protected DriverInterface $displayNameDriver A driver for getting display names.
static protected Hasher $hasher The hasher with which to hash passwords.
static protected Gate $gate The access gate.
static protected array $passwordCheckers Callbacks to check passwords.
int $id
string $username
string $display_name
string $email
bool $is_email_confirmed
string $password
string|null $avatar_url
Carbon|null $joined_at
Carbon|null $last_seen_at
Carbon|null $marked_all_as_read_at
Carbon|null $read_notifications_at
int $discussion_count
int $comment_count

Methods

static 
boot()

Boot the model.

__construct(array $attributes = [])

{@inheritdoc}

array
getDates()

Get the attributes that should be converted to dates.

mixed
getAttribute(string $key)

Get an attribute from the model. If nothing is found, attempt to load a custom relation method with this key.

mixed
getCustomRelation(string $name)

Get a custom relation object.

void
afterSave(callable $callback)

Register a callback to be run once after the model is saved.

void
afterDelete(callable $callback)

Register a callback to be run once after the model is deleted.

callable[]
releaseAfterSaveCallbacks()

No description

callable[]
releaseAfterDeleteCallbacks()

No description

__call($method, $arguments)

{@inheritdoc}

raise(mixed $event)

Raise a new event.

array
releaseEvents()

Return and reset all pending events.

static 
registerVisibilityScoper($scoper, $ability = null)

No description

scopeWhereVisibleTo(Builder $query, User $actor, string $ability = 'view')

Scope a query to only include records that are visible to a user.

static User
register(string $username, string $email, string $password)

Register a new user.

static 
setGate(Gate $gate)

No description

static 
setDisplayNameDriver(DriverInterface $driver)

Set the display name driver.

static 
setPasswordCheckers(array $checkers)

No description

$this
rename(string $username)

Rename the user.

$this
changeEmail(string $email)

Change the user's email.

$this
requestEmailChange(string $email)

Request that the user's email be changed.

$this
changePassword(string $password)

Change the user's password.

setPasswordAttribute(string $value)

Set the password attribute, storing it as a hash.

$this
markAllAsRead()

Mark all discussions as read.

$this
markNotificationsAsRead()

Mark all notifications as read.

$this
changeAvatarPath(string $path)

Change the path of the user avatar.

string
getAvatarUrlAttribute(string $value = null)

Get the URL of the user's avatar.

string
getDisplayNameAttribute()

Get the user's display name.

bool
checkPassword(string $password)

Check if a given password matches the user's password.

$this
activate()

Activate the user's account.

bool
hasPermission(string $permission)

Check whether the user has a certain permission based on their groups.

bool
hasPermissionLike(string $match)

Check whether the user has a permission that is like the given string, based on their groups.

array
getAlertableNotificationTypes()

Get the notification types that should be alerted to this user, according to their preferences.

int
getUnreadNotificationCount()

Get the number of unread notifications for the user.

Collection
getUnreadNotifications()

Get all notifications that have not been read yet.

int
getNewNotificationCount()

Get the number of new, unseen notifications for the user.

array
getPreferencesAttribute(string $value)

Get the values of all registered preferences for this user, by transforming their stored preferences and merging them with the defaults.

setPreferencesAttribute(mixed $value)

Encode an array of preferences for storage in the database.

bool
shouldAlert(string $type)

Check whether or not the user should receive an alert for a notification type.

bool
shouldEmail(string $type)

Check whether or not the user should receive an email for a notification type.

mixed
getPreference(string $key, mixed $default = null)

Get the value of a preference for this user.

$this
setPreference(string $key, mixed $value)

Set the value of a preference for this user.

$this
updateLastSeen()

Set the user as being last seen just now.

bool
isAdmin()

Check whether or not the user is an administrator.

bool
isGuest()

Check whether or not the user is a guest.

assertPermission(bool $condition)

Ensure the current user is allowed to do something.

assertRegistered()

Ensure the given actor is authenticated.

assertCan(string $ability, mixed $arguments = null)

No description

assertAdmin()

No description

HasMany
posts()

Define the relationship with the user's posts.

HasMany
discussions()

Define the relationship with the user's discussions.

BelongsTo
read()

Define the relationship with the user's read discussions.

BelongsToMany
groups()

Define the relationship with the user's groups.

visibleGroups()

No description

HasMany
notifications()

Define the relationship with the user's notifications.

HasMany
emailTokens()

Define the relationship with the user's email tokens.

Builder
permissions()

Define the relationship with the permissions of all of the groups that the user is in.

string[]
getPermissions()

Get a list of permissions that the user has.

HasMany
accessTokens()

Define the relationship with the user's access tokens.

loginProviders()

Get the user's login providers.

bool
can(string $ability, array|mixed $arguments = null)

No description

bool
cannot(string $ability, array|mixed $arguments = null)

No description

Session
getSession()

No description

setSession(Session $session)

No description

static 
setHasher(Hasher $hasher)

Set the hasher with which to hash passwords.

static 
registerPreference(string $key, callable $transformer = null, mixed $default = null)

Register a preference with a transformer and a default value.

static array
addGroupProcessor(callable $callback)

Register a callback that processes a user's list of groups.

static string
getNotificationPreferenceKey(string $type, string $method)

Get the key for a preference which flags whether or not the user will receive a notification for $type via $method.

$this
refreshCommentCount()

Refresh the user's comments count.

$this
refreshDiscussionCount()

Refresh the user's comments count.

Details

static boot()

Boot the model.

__construct(array $attributes = [])

{@inheritdoc}

Parameters

array $attributes

array getDates()

Get the attributes that should be converted to dates.

Return Value

array

mixed getAttribute(string $key)

Get an attribute from the model. If nothing is found, attempt to load a custom relation method with this key.

Parameters

string $key

Return Value

mixed

protected mixed getCustomRelation(string $name)

Get a custom relation object.

Parameters

string $name

Return Value

mixed

void afterSave(callable $callback)

Register a callback to be run once after the model is saved.

Parameters

callable $callback

Return Value

void

void afterDelete(callable $callback)

Register a callback to be run once after the model is deleted.

Parameters

callable $callback

Return Value

void

callable[] releaseAfterSaveCallbacks()

Return Value

callable[]

callable[] releaseAfterDeleteCallbacks()

Return Value

callable[]

__call($method, $arguments)

{@inheritdoc}

Parameters

$method
$arguments

raise(mixed $event)

Raise a new event.

Parameters

mixed $event

array releaseEvents()

Return and reset all pending events.

Return Value

array

static registerVisibilityScoper($scoper, $ability = null)

Parameters

$scoper
$ability

scopeWhereVisibleTo(Builder $query, User $actor, string $ability = 'view')

Scope a query to only include records that are visible to a user.

Parameters

Builder $query
User $actor
string $ability

static User register(string $username, string $email, string $password)

Register a new user.

Parameters

string $username
string $email
string $password

Return Value

User

static setGate(Gate $gate)

Parameters

Gate $gate

static setDisplayNameDriver(DriverInterface $driver)

Set the display name driver.

Parameters

DriverInterface $driver

static setPasswordCheckers(array $checkers)

Parameters

array $checkers

$this rename(string $username)

Rename the user.

Parameters

string $username

Return Value

$this

$this changeEmail(string $email)

Change the user's email.

Parameters

string $email

Return Value

$this

$this requestEmailChange(string $email)

Request that the user's email be changed.

Parameters

string $email

Return Value

$this

$this changePassword(string $password)

Change the user's password.

Parameters

string $password

Return Value

$this

setPasswordAttribute(string $value)

Set the password attribute, storing it as a hash.

Parameters

string $value

$this markAllAsRead()

Mark all discussions as read.

Return Value

$this

$this markNotificationsAsRead()

Mark all notifications as read.

Return Value

$this

$this changeAvatarPath(string $path)

Change the path of the user avatar.

Parameters

string $path

Return Value

$this

string getAvatarUrlAttribute(string $value = null)

Get the URL of the user's avatar.

Parameters

string $value

Return Value

string

string getDisplayNameAttribute()

Get the user's display name.

Return Value

string

bool checkPassword(string $password)

Check if a given password matches the user's password.

Parameters

string $password

Return Value

bool

$this activate()

Activate the user's account.

Return Value

$this

bool hasPermission(string $permission)

Check whether the user has a certain permission based on their groups.

Parameters

string $permission

Return Value

bool

bool hasPermissionLike(string $match)

Check whether the user has a permission that is like the given string, based on their groups.

Parameters

string $match

Return Value

bool

array getAlertableNotificationTypes()

Get the notification types that should be alerted to this user, according to their preferences.

Return Value

array

int getUnreadNotificationCount()

Get the number of unread notifications for the user.

Return Value

int

protected Collection getUnreadNotifications()

Get all notifications that have not been read yet.

Return Value

Collection

int getNewNotificationCount()

Get the number of new, unseen notifications for the user.

Return Value

int

array getPreferencesAttribute(string $value)

Get the values of all registered preferences for this user, by transforming their stored preferences and merging them with the defaults.

Parameters

string $value

Return Value

array

setPreferencesAttribute(mixed $value)

Encode an array of preferences for storage in the database.

Parameters

mixed $value

bool shouldAlert(string $type)

Check whether or not the user should receive an alert for a notification type.

Parameters

string $type

Return Value

bool

bool shouldEmail(string $type)

Check whether or not the user should receive an email for a notification type.

Parameters

string $type

Return Value

bool

mixed getPreference(string $key, mixed $default = null)

Get the value of a preference for this user.

Parameters

string $key
mixed $default

Return Value

mixed

$this setPreference(string $key, mixed $value)

Set the value of a preference for this user.

Parameters

string $key
mixed $value

Return Value

$this

$this updateLastSeen()

Set the user as being last seen just now.

Return Value

$this

bool isAdmin()

Check whether or not the user is an administrator.

Return Value

bool

bool isGuest()

Check whether or not the user is a guest.

Return Value

bool

assertPermission(bool $condition)

Ensure the current user is allowed to do something.

If the condition is not met, an exception will be thrown that signals the lack of permissions. This is about <em>authorization</em>, i.e. retrying such a request / operation without a change in permissions (or using another user account) is pointless.

Parameters

bool $condition

Exceptions

PermissionDeniedException

assertRegistered()

Ensure the given actor is authenticated.

This will throw an exception for guest users, signaling that <em>authorization</em> failed. Thus, they could retry the operation after logging in (or using other means of authentication).

Exceptions

NotAuthenticatedException

assertCan(string $ability, mixed $arguments = null)

Parameters

string $ability
mixed $arguments

Exceptions

PermissionDeniedException

assertAdmin()

Exceptions

PermissionDeniedException

HasMany posts()

Define the relationship with the user's posts.

Return Value

HasMany

HasMany discussions()

Define the relationship with the user's discussions.

Return Value

HasMany

BelongsTo read()

Define the relationship with the user's read discussions.

Return Value

BelongsTo

BelongsToMany groups()

Define the relationship with the user's groups.

Return Value

BelongsToMany

visibleGroups()

HasMany notifications()

Define the relationship with the user's notifications.

Return Value

HasMany

HasMany emailTokens()

Define the relationship with the user's email tokens.

Return Value

HasMany

Builder permissions()

Define the relationship with the permissions of all of the groups that the user is in.

Return Value

Builder

string[] getPermissions()

Get a list of permissions that the user has.

Return Value

string[]

HasMany accessTokens()

Define the relationship with the user's access tokens.

Return Value

HasMany

loginProviders()

Get the user's login providers.

bool can(string $ability, array|mixed $arguments = null)

Parameters

string $ability
array|mixed $arguments

Return Value

bool

bool cannot(string $ability, array|mixed $arguments = null)

Parameters

string $ability
array|mixed $arguments

Return Value

bool

Session getSession()

Return Value

Session

setSession(Session $session)

Parameters

Session $session

static setHasher(Hasher $hasher)

Set the hasher with which to hash passwords.

Parameters

Hasher $hasher

static registerPreference(string $key, callable $transformer = null, mixed $default = null)

Register a preference with a transformer and a default value.

Parameters

string $key
callable $transformer
mixed $default

static array addGroupProcessor(callable $callback)

Register a callback that processes a user's list of groups.

Parameters

callable $callback

Return Value

array $groupIds

static string getNotificationPreferenceKey(string $type, string $method)

Get the key for a preference which flags whether or not the user will receive a notification for $type via $method.

Parameters

string $type
string $method

Return Value

string

$this refreshCommentCount()

Refresh the user's comments count.

Return Value

$this

$this refreshDiscussionCount()

Refresh the user's comments count.

Return Value

$this