Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ehabafia's avatar

Notifications are processed twice

Dear all,

I am trying to use $model->notify(new Notification); as I always do, but with this project I am using Laravel version 9.52.14

It does sends the notification email normally (once), but when I tried to ray("Hello"); inside the notification class toMail() method, I am having the word Hello recorded twice in Ray.

Which means any record I am trying to create from within that method it's recording twice, although the email is sent once.

Any thoughts?

Thank you,

0 likes
12 replies
LaryAI's avatar
Level 58

This issue might be caused by the notification being sent twice due to some misconfiguration or duplicate event listeners. Here are a few steps you can take to troubleshoot and resolve the problem:

  1. Check for duplicate event listeners: Make sure that you don't have multiple event listeners registered for the same event. Check your event service provider (EventServiceProvider) and any other custom event listeners you might have created. Remove any duplicate listeners that might be causing the notification to be processed twice.

  2. Verify the notification usage: Double-check how you are triggering the notification. Ensure that you are not accidentally calling the notify method twice or triggering the notification from multiple places in your code.

  3. Review the notification class: Take a closer look at the Notification class that you are using. Make sure that you don't have any logic or code that might be causing the duplication. Check the toMail method and any other methods or traits used by the notification. Ensure that you are not inadvertently duplicating the logic or creating multiple instances of the notification.

If the issue persists after following these steps, it might be helpful to provide more details about your code, including the Notification class and any relevant event listeners, so that we can assist you further.

vincent15000's avatar

It's not due to the notification itself but to the way you send the notification.

How do you send the notification ? Is it via an event ? Directly in a controller ? How is the mail stored in the database ?

And what is Ray ? You don't explain this in your post.

ehabafia's avatar

@vincent15000 I sent the notification via the model, like:

$borrower->notify(new NotificationEmail);

ray is the spatie application that record whatever you use ray with. something like dump();

1 like
vincent15000's avatar

@ehabafia I would comment this line and check if the notification is sent. This would allow you to know if there is another place where the notification is sent.

ehabafia's avatar

@vincent15000 The problem is that the email got sent once, but the storing the record twice. and when I commented it out, it doesn't store any.

The problem is that the Model store record is via the method viaMail() which is the same method that send the email. The email got sent once, while the storing the record to the DB twice.

1 like
eMiracle's avatar

@ehabafia I noticed this recently, it seems to happen on the live environment only

1 like
krisi_gjika's avatar

@ehabafia @mohamedtammam notifications are not the place to create records, there is no guarantee how many times a method of the notification class may be called for internal functionality. If you need your notification to do more, wrap it in a job or something.

1 like
eugenefvdm's avatar

I just had a similar issue. Ray was repeating the notification which is output from Pest.

I would normally just ignore this and write it off as something weird, but in my instance I know something else is was wrong because I have logic in Pest to count a database value.

This database value is being incremented in the event listener system and more specifically the NotificationSent built-in event.

My automated Pest tests failed because Pest indicated the event was fired twice but I definitely only increased a database value once in the event.

In the end this was the culprit in the notifiable:

public function via(object $notifiable): array
    {
        return ['mail', 'database'];
    }

Once I removed database it stopped the second Ray notify output.

1 like

Please or to participate in this conversation.