Skip to content

Commit

Permalink
[8.x] Add read / unread scopes to database notifications (#35215)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 13, 2020
1 parent e9483c4 commit 72d6f88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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();
}
}

0 comments on commit 72d6f88

Please sign in to comment.