I have an application build in Laravel where it sends notifications to users.
for Example when a new Post is created it sends notification to multiple users based on some conditions, the problem I'm facing is when multiple posts are create in small period of time for example 30 Minutes I don't want the user to get notified by lots of notifications, I just want to update the count in last notification.
but I need to prevent creation of a new notification when I update the last notification.
here is what I have written.
public function toDatabase($notifiable): array
$count = 1;
if($notification = $notifiable->notifications()->where('data->search', $this->search->id)
->where('updated_at', '>=', now()->subMinutes(30))
->first()) {
$count = isset($notification->data['count']) ? $notification->data['count'] + 1 : 1;
$notification->update([
'data' => [
'content' => [
'en' => "{$count} new posts matched with your saved search {$this->search->title} has been posted, Press here to view more.",
],
'count' => $count
]
]);
}
return [
'content' => [
'en' => "{$count} new post matched with your saved search {$this->search->title} has been posted, Press here to view more.",
],
'count' => $count,
'search' => $this->search->id,
'parameters' => $this->search->parameters
];
}
How to prevent the code from reaching to return [] ?