Preview your notifications easily with just a couple of lines of code
Sometimes you want to develop beautiful notifications in HTML or markdown format. But how do you style them easily without having to send them every time you've changed something? Laravel offers an out of the box solution for this. In your web.php you can easily return a notification to render it and display it in your web browser.
Let's code!
If you'd like to try this with an easy example you can code along with me. First, we start with creating an example notification:
$ php artisan make:notification InvoicePaid
Secondly, we add the following lines to our web.php
to be able to display the notification in our browser:
As the title of this article might confuse, it sounds like we render a Notification, in this case, InvoicePaid. But what we're actually rendering here is the MailMessage
we return in the toMail
method.
Rendering markdown notifications
Laravel supports sending markdown notifications as well and it would be nice to be able to preview these as well in the browser. For this kind of notifications it requires a little bit more code:
In this case, we use the \Illuminate\Mail\Markdown
class to render the markdown to HTML. Go to /notification
in your browser to see if it works!
(Bonus!) Rendering On-Demand Notifications
All objects which are using the Notifiable
trait can be notified through their desired channel. This is a great out of the box feature from Laravel. But there are times that you'd like to send a notification to an email or mobile number which isn't a Notifiable instance (yet). Laravel calls these On-Demand Notifications. To render On-Demand Notifications in your browser we have the following workaround:
In the background, Laravel uses the AnonymousNotifiable
class to send notifications on-demand.
Top comments (0)