DEV Community

Sjors van Dongen
Sjors van Dongen

Posted on

6 3

Previewing Laravel markdown Notifications directly in your browser

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:

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
$router->get('/notification', function () {
$notification = new \App\Notifications\InvoicePaid;
return $notification->toMail('test@example.com');
});
view raw web.php hosted with ❤ by GitHub

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:

<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
$router->get('/notification', function () {
$notification = (new \App\Notifications\InvoicePaid)->toMail('test@example.com');
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render($notification->markdown, $notification->data());
});
view raw web.php hosted with ❤ by GitHub

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:

<?php
use Illuminate\Notifications\AnonymousNotifiable;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
$router->get('/notification', function () {
$user = new AnonymousNotifiable;
$notification = (new \App\Notifications\InvoicePaid)->toMail($user->route('mail', ['test@example.nl']));
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render($notification->markdown, $notification->data());
});
view raw web.php hosted with ❤ by GitHub

In the background, Laravel uses the AnonymousNotifiable class to send notifications on-demand. 


I'm currently working on Laravel Pigeon. A notification center for all your communication with customers AND for debugging notifications in your browser!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay