master

laravel/framework

Last updated at: 29/12/2023 09:23

DatabaseNotification.php

TLDR

The DatabaseNotification.php file is a part of the Illuminate\Notifications namespace and defines the DatabaseNotification class, which extends the Model class. This class represents a notification stored in the database. It provides methods for marking notifications as read or unread, determining if a notification has been read, and scoping queries to include read or unread notifications.

Methods

notifiable

This method defines a relationship between the DatabaseNotification model and the notifiable entity that the notification belongs to. It returns an instance of the MorphTo class.

markAsRead

This method marks the notification as read by setting the read_at attribute to the current timestamp. If the notification is already marked as read, it does nothing.

markAsUnread

This method marks the notification as unread by setting the read_at attribute to null. If the notification is already marked as unread, it does nothing.

read

This method determines if the notification has been read by checking if the read_at attribute is not null. It returns a boolean value.

unread

This method determines if the notification has not been read by checking if the read_at attribute is null. It returns a boolean value.

scopeRead

This method is a query scope that can be used to filter notifications that have been read. It accepts a Builder instance as a parameter and adds a condition to the query to include only notifications where the read_at attribute is not null. It returns a Builder instance.

scopeUnread

This method is a query scope that can be used to filter notifications that have not been read. It accepts a Builder instance as a parameter and adds a condition to the query to include only notifications where the read_at attribute is null. It returns a Builder instance.

newCollection

This method creates a new instance of the DatabaseNotificationCollection class, which extends the Collection class. It is used to initialize a collection of DatabaseNotification models. It accepts an optional array of models as a parameter and returns a new instance of DatabaseNotificationCollection.

<?php

namespace Illuminate\Notifications;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class DatabaseNotification extends Model
{
    /**
     * The "type" of the primary key ID.
     *
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'notifications';

    /**
     * The guarded attributes on the model.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'data' => 'array',
        'read_at' => 'datetime',
    ];

    /**
     * Get the notifiable entity that the notification belongs to.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function notifiable()
    {
        return $this->morphTo();
    }

    /**
     * Mark the notification as read.
     *
     * @return void
     */
    public function markAsRead()
    {
        if (is_null($this->read_at)) {
            $this->forceFill(['read_at' => $this->freshTimestamp()])->save();
        }
    }

    /**
     * Mark the notification as unread.
     *
     * @return void
     */
    public function markAsUnread()
    {
        if (! is_null($this->read_at)) {
            $this->forceFill(['read_at' => null])->save();
        }
    }

    /**
     * Determine if a notification has been read.
     *
     * @return bool
     */
    public function read()
    {
        return $this->read_at !== null;
    }

    /**
     * Determine if a notification has not been read.
     *
     * @return bool
     */
    public function unread()
    {
        return $this->read_at === null;
    }

    /**
     * Scope a query to only include read notifications.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeRead(Builder $query)
    {
        return $query->whereNotNull('read_at');
    }

    /**
     * Scope a query to only include unread notifications.
     *
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeUnread(Builder $query)
    {
        return $query->whereNull('read_at');
    }

    /**
     * Create a new database notification collection instance.
     *
     * @param  array  $models
     * @return \Illuminate\Notifications\DatabaseNotificationCollection
     */
    public function newCollection(array $models = [])
    {
        return new DatabaseNotificationCollection($models);
    }
}