Querying the notification log | 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  Querying the notification log

 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)

 Querying the notification log
=============================

###  On this page

1. [ Getting the latest log item for a notifiable ](#content-getting-the-latest-log-item-for-a-notifiable)
2. [ Determining sent notifications within a notification ](#content-determining-sent-notifications-within-a-notification)

For each notification sent in your app, a row in the `notification_log_items` table will be created. 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();
```

Getting the latest log item for a notifiable
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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 that corresponds 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 notification type that was most recently sent.

### Using the notifiable to search logged notifications

The package provides a trait `Spatie\NotificationLog\Models\Concerns\HasNotifiableHistory` that you can use on a notifiable.

```
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use Notifiable;
    use HasNotifiableHistory;
}
```

This trait will add a method `notificationLogItems` that will return a `MorphMany` relation to all notifications that were logged for the notifiable.

```
/*
 * return all logged notifications, the most recent are first
 */
$loggedNotifications = $user->notificationLogItems;

/*
 * return the five most recent logged notifications
 */
$loggedNotifications = $user->notificationLogItems()->limit(5)->get();
```

Additionally, the trait provides a `latestLoggedNotification` method that will return the latest logged notification for the 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 = $user->latestLoggedNotification();
```

The method has a couple of optional parameters to search for the latest log item that corresponds to the given restrictions.

```
use App\Notifications\OrderSentNotification;

$logItem = $user->latestLoggedNotification(
    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 notification type that was most recently sent.

Determining sent notifications within 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\Notification;
use 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);
}
```

By default, `wasNotSentTo` will not take into account [the fingerprint of a logged notification](/docs/laravel-notification-log/v1/basic-usage/working-with-fingerprints). To take it into account, set the named argument `withSameFingerprint` to true.

```
public function shouldSend($notifiable)
{
    $this
       ->wasNotSentTo($notifiable, withSameFingerprint: true)
       ->inThePastMinutes(30);
}
```

In addition to `wasNotSentTo`, the trait also has a method `wasSentTo`.
