Working with fingerprints | laravel-notification-log | Spatie

 SPATIE

  Laravel Notification Log
===========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-notification-log](https://spatie.be/docs/laravel-notification-log/v1)  Basic-usage  Working with fingerprints

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-notification-log/v1)

- [ Introduction ](https://spatie.be/docs/laravel-notification-log/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-notification-log/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-notification-log/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-notification-log/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-notification-log/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-notification-log/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-notification-log/v1/about-us)

Basic usage
-----------

- [ Getting started ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/getting-started)
- [ Determining which notifications get logged ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/determining-which-notifications-get-logged)
- [ Adding extra info to a logged notifications ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/adding-extra-info-to-your-notification)
- [ Querying the notification log ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/querying-the-notification-log)
- [ Working with fingerprints ](https://spatie.be/docs/laravel-notification-log/v1/basic-usage/working-with-fingerprints)

Advanced usage
--------------

- [ Customizing notification types ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/customizing-notification-types)
- [ Customizing the logging process ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/customizing-the-logging-process)
- [ Handling on demand notifications ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/handling-on-demand-notifications)
- [ Using your own notification log model ](https://spatie.be/docs/laravel-notification-log/v1/advanced-usage/using-your-own-notification-log-model)

 Working with fingerprints
=========================

Notification typically accept parameters in the constructor that will be used to determine the message of the notification.

```
use Illuminate\Notifications\Notification;

class OrderSentNotification extends Notification
{
    public function __construct(
        public Order $order,
    ) {}
```

To log that a notification was sent for a set of constructor parameters, you can add a fingerprint. A fingerprint is a simple string that will be logged along with the sent notification.

To add a signature to a notification, add a function `fingerprint` to your notification.

```
use Illuminate\Notifications\Notification;

class OrderSentNotification extends Notification
{
    public function __construct(
        public Order $order,
    ) {}

    public function fingerprint()
    {
        return "order-{$this->order->id}";
    }
}
```

The fingerprint will be saved in the `fingerprint` on the log item that is created when the notification is sent.

```
// returns the fingerprint
Spatie\NotificationLog\Models\NotificationLogItem::first()->fingerprint;
```

You can use the fingerprint to hunt down notifications using [the `latestFor` method](/docs/laravel-notification-log/v1/basic-usage/querying-the-notification-log).

```
// return the latest logged notification for the first order.

$logItem = NotificationLogItem::latestFor($notifiable, fingerprint: "order-1");
```

When your notification has a lot, or complex, constructor parameters, you could use a hashing function like `md5` to generate a value that is unique for those parameters.

```
use Illuminate\Notifications\Notification;

class OrderSentNotification extends Notification
{
    public function __construct(
        public string $parameter,
        public string $anotherParameter,
        public string $yetAnotherOne,

    ) {}

    public function fingerprint()
    {
        return md5("{$this-parameter}-{$this->anotherParameter}-{$yetAnotherOne}";
    }
}
```
