designtopack/public/site/plugins/notifications/user-methods/read.php
2024-11-18 09:36:15 +01:00

26 lines
No EOL
718 B
PHP

<?php
return function($notificationId) {
try {
$notifications = $this->notifications()->isNotEmpty()
? Yaml::decode($this->notifications()->value())
: [];
$newNotification = null;
foreach ($notifications as $key => $notification) {
if ($notification['id'] === $notificationId) {
$notifications[$key]['isRead'] = true;
$newNotification = $notifications[$key];
}
}
$this->update([
'notifications' => Yaml::encode(array_values($notifications))
]);
return $newNotification;
} catch (\Throwable $th) {
throw new Exception("Error updating notifications: " . $th->getMessage() . ' line ' . $th->getLine(), 1);
}
};