Created
February 10, 2024 18:15
-
-
Save malkafly/981b4f183bb9be7c5f90eb5e1deb9aca to your computer and use it in GitHub Desktop.
Using MongoDB for store notifications on Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Overrides\Notifications; | |
use MongoDB\Laravel\Eloquent\Model; | |
use MongoDB\Laravel\Eloquent\Builder; | |
//use Illuminate\Database\Eloquent\Builder; | |
use Illuminate\Notifications\DatabaseNotificationCollection; | |
class DatabaseNotification extends Model | |
{ | |
protected $connection = 'mongodb'; | |
//protected $collection = 'user_interviews'; | |
/** | |
* 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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Overrides\Notifications; | |
trait HasDatabaseNotifications | |
{ | |
/** | |
* Get the entity's notifications. | |
* | |
* @return \Illuminate\Database\Eloquent\Relations\MorphMany | |
*/ | |
public function notifications() | |
{ | |
return $this->morphMany(DatabaseNotification::class, 'notifiable')->latest(); | |
} | |
/** | |
* Get the entity's read notifications. | |
* | |
* @return \Illuminate\Database\Query\Builder | |
*/ | |
public function readNotifications() | |
{ | |
return $this->notifications()->read(); | |
} | |
/** | |
* Get the entity's unread notifications. | |
* | |
* @return \Illuminate\Database\Query\Builder | |
*/ | |
public function unreadNotifications() | |
{ | |
return $this->notifications()->unread(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Overrides\Notifications; | |
use Illuminate\Notifications\RoutesNotifications; | |
trait Notifiable | |
{ | |
use HasDatabaseNotifications, RoutesNotifications; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment