Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add read / unread scopes to database notifications #35215

Merged
merged 1 commit into from Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Illuminate/Notifications/DatabaseNotification.php
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Notifications;

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

class DatabaseNotification extends Model
Expand Down Expand Up @@ -98,6 +99,28 @@ 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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Notifications/HasDatabaseNotifications.php
Expand Up @@ -21,7 +21,7 @@ public function notifications()
*/
public function readNotifications()
{
return $this->notifications()->whereNotNull('read_at');
return $this->notifications()->read();
}

/**
Expand All @@ -31,6 +31,6 @@ public function readNotifications()
*/
public function unreadNotifications()
{
return $this->notifications()->whereNull('read_at');
return $this->notifications()->unread();
}
}