FORMATTING DATABASE NOTIFICATIONS

Mar 08, 2024 Copy Link

I was working on a project where I was sending notifications to the Front-end via the database, so I defined `toArray` and `toDatabase` methods in the notification class where there was a specific logic in the `toArray` that must be accomplished, but every time I found that Laravel did not care about this method 🤯

 

After I read more about these two methods in the docs, I realized that I should define these two methods only when I have a multi-channel for notifications where the database channel must be one of them, only in this case, Laravel will use the `toDatabase` method when sending the notification via the database, and the other channels will use the `toArray` and since I was using only one channel, which is the database, I had to use one of these methods, so when I removed the `toDatabase` method, Laravel finally walked through the `toArray` and the logic was executed ✅

Share via

Filed in:

Tips Digging Deeper
Mahmoud Ramadan

Mahmoud Ramadan

Mahmoud is the creator of Digging Code and a contributor to Laravel since 2020.

Newly published

  • How to Enable Relationship Autoloading in Versions Before v12.8

    How to Enable Relationship Autol...

    FREE

  • Get your environment ready to welcome Laravel v12

    Get your environment ready to we...

    FREE

  • How to generate Arabic PDF using TCPDF

    How to generate Arabic PDF using...

    FREE