class Notification extends AbstractModel

Models a notification record in the database.

A notification record is associated with a user, and shows up in their notification list. A notification indicates that something has happened that the user should know about, like if a user's discussion was renamed by someone else.

Each notification record has a type. The type determines how the record looks in the notifications list, and what subject is associated with it. For example, the 'discussionRenamed' notification type represents that someone renamed a user's discussion. Its subject is a discussion, of which the ID is stored in the subject_id column.

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 $dates The attributes that should be mutated to dates.
static protected array $subjectModels A map of notification types and the model classes to use for their subjects. For example, the 'discussionRenamed' notification type, which represents that a user's discussion was renamed, has the subject model class 'Flarum\Discussion\Discussion'.
int $id
int $user_id
int|null $from_user_id
string $type
int|null $subject_id
mixed|null $data
Carbon $created_at
Carbon $read_at
Carbon $deleted_at
User|null $user
User|null $fromUser
AbstractModel|null $subject

Methods

static 
boot()

{@inheritdoc}

__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}

void
read()

Mark a notification as read.

mixed
getDataAttribute(string $value)

When getting the data attribute, unserialize the JSON stored in the database into a plain array.

setDataAttribute(mixed $value)

When setting the data attribute, serialize it into JSON for storage in the database.

string|null
getSubjectModelAttribute()

Get the subject model for this notification record by looking up its type in our subject model map.

BelongsTo
user()

Define the relationship with the notification's recipient.

BelongsTo
fromUser()

Define the relationship with the notification's sender.

MorphTo
subject()

Define the relationship with the notification's subject.

Builder
scopeWhereSubjectVisibleTo(Builder $query, User $actor)

Scope the query to include only notifications whose subjects are visible to the given user.

Builder
scopeWhereSubject(Builder $query, object $model)

Scope the query to include only notifications that have the given subject.

Builder
scopeWhereSubjectModel(Builder $query, string $class)

Scope the query to include only notification types that use the given subject model.

Builder
scopeMatchingBlueprint(Builder $query, BlueprintInterface $blueprint)

Scope the query to find all records matching the given blueprint.

static 
notify(array $recipients, BlueprintInterface $blueprint)

Send notifications to the given recipients.

static array
getSubjectModels()

Get the type-to-subject-model map.

static void
setSubjectModel(string $type, string $subjectModel)

Set the subject model for the given notification type.

Details

static boot()

{@inheritdoc}

__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

void read()

Mark a notification as read.

Return Value

void

mixed getDataAttribute(string $value)

When getting the data attribute, unserialize the JSON stored in the database into a plain array.

Parameters

string $value

Return Value

mixed

setDataAttribute(mixed $value)

When setting the data attribute, serialize it into JSON for storage in the database.

Parameters

mixed $value

string|null getSubjectModelAttribute()

Get the subject model for this notification record by looking up its type in our subject model map.

Return Value

string|null

BelongsTo user()

Define the relationship with the notification's recipient.

Return Value

BelongsTo

BelongsTo fromUser()

Define the relationship with the notification's sender.

Return Value

BelongsTo

MorphTo subject()

Define the relationship with the notification's subject.

Return Value

MorphTo

Builder scopeWhereSubjectVisibleTo(Builder $query, User $actor)

Scope the query to include only notifications whose subjects are visible to the given user.

Parameters

Builder $query
User $actor

Return Value

Builder

Builder scopeWhereSubject(Builder $query, object $model)

Scope the query to include only notifications that have the given subject.

Parameters

Builder $query
object $model

Return Value

Builder

Builder scopeWhereSubjectModel(Builder $query, string $class)

Scope the query to include only notification types that use the given subject model.

Parameters

Builder $query
string $class

Return Value

Builder

Builder scopeMatchingBlueprint(Builder $query, BlueprintInterface $blueprint)

Scope the query to find all records matching the given blueprint.

Parameters

Builder $query
BlueprintInterface $blueprint

Return Value

Builder

static notify(array $recipients, BlueprintInterface $blueprint)

Send notifications to the given recipients.

Parameters

array $recipients
BlueprintInterface $blueprint

static array getSubjectModels()

Get the type-to-subject-model map.

Return Value

array

static void setSubjectModel(string $type, string $subjectModel)

Set the subject model for the given notification type.

Parameters

string $type The notification type.
string $subjectModel The class name of the subject model for that type.

Return Value

void