Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

Log all notifications sent by your Laravel app

Original – by Freek Van der Herten – 4 minute read

We've released a new package called Laravel Notification Log. True to its name, it will log when your Laravel app sends notifications. It also contains helpful methods to determine when notifications were sent.

Using the package

By default, the package will write an entry in the notification_log_items table for each notification sent in your app. You can also have fine-grained control over which notifications should be logged.

The notification_log_items table has these columns:

  • notification_type: the class name of the sent notification
  • notifiable_id: the key value of the notifiable the notification was sent to
  • notifiable_type: the (morph) class name of the notifiable the notification was sent to
  • channel: the name of the channel the notification was sent to
  • fingerprint: a value that you can customize to identify the exact content of the notification
  • extra: an array of values you can freely add to the log
  • anonymous_notifiable_properties: the configuration of the notifiable when sending an on-demand notification
  • confirmed_at: will contain the time the NotificationSent event was fired

How the package fills all these properties can be fully customized.

You can use the Spatie\NotificationLog\Models\NotificationLogItem model to query logged notifications.

use Spatie\NotificationLog\Models\NotificationLogItem;

// returns all logged notifications
NotificationLogItem::query()->orderByDesc('id')->get();

The model has a handy latestFor method that will return the single latest logged notification for a given notifiable.

/*
 * Will return the single most recent sent log item for the given notifiable.
 * If there was no notification sent yet to the notifiable, `null` will be returned.
 */
$logItem = NotificationLogItem::latestFor($notifiable);

The latestFor has a couple of optional parameters to search for the latest log item corresponding to the given restrictions.

use Spatie\NotificationLog\Models\NotificationLogItem;
use App\Notifications\OrderSentNotification;

$logItem = NotificationLogItem::latestFor(
    $notifiable,
    notificationType: OrderSentNotification::class, // search for a specific notification type
    before: $carbon, // we're looking for a notification before the given carbon instance
    after: $carbon, // we're looking for a notification after the given carbon instance
    fingerprint: 'dummy-fingerprint' // search for a log item with this fingerprint
);

The notificationType parameter can be an array, in which case latestFor will return a log item with the notification type that was most recently sent.

Getting the notifications for a user

You can add the HasNotifiableHistory trait on a notifiable. This trait contains a few methods to work the notification history.

You should apply it to a notifiable, such as a User model.

use Spatie\NotificationLog\Models\Concerns\HasNotifiableHistory;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
   use HasNotifiableHistory;
}

Here's how to use the log to render a view with all sent notifications for a user.

<ul>
@foreach($user->loggedNotifications() as $sentNotification)
    <li>{{ $sentNotification->type }} at {{ $sentNotification->created_at->format('Y-m-d H:i:s') }}</li>
@endforeach
</ul>

Using history on a notification

The package offers a HasHistory trait that contains a couple of functions that allow you to determine if a similar notification was recently sent.

To get started, first add the trait to your notification.

namespace App\Notifications;

use Illuminate\Notifications\Notificationuse Spatie\NotificationLog\Models\Concerns\HasHistory;

class YourNotification extends Notification
{
    use HasHistory;
}

Imagine that your notification should only be sent if a similar notification wasn't recently sent.

public function shouldSend($notifiable)
{
    return $this
       ->wasNotSentTo($notifiable)
       ->inThePastMinutes(30);
}

In closing

The Laravel Notification Log package contains will log all notifications by your app, and offers handy methods to query to log. The package has more options. Learn them in our extensive docs.

This is one of many packages our team has made. Please look at this extensive list of Laravel and PHP packages we've made before. I'm sure there's something there for your next project. If you want to support our open-source efforts, consider picking up one of our paid products or subscribe at Mailcoach and/or Flare.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Log all notifications sent by your Laravel app"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.