0

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 [] ?

1 Answers1

0

maybe add another condition, maybe in your database add an column Last_send and when executing it compares the current time from Last_send if time is greater than 30min you let it pass.

Rick W
  • 75
  • 7
  • I actually fixed it by moving the code to "via" method, and if the condition passed I return empty array, otherwise return the ['database'] – Mahmoud Ben Jabir Dec 15 '21 at 18:19