<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jamesmills / laravel-notification-rate-limit example snippets
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Jamesmills\LaravelNotificationRateLimit\RateLimitedNotification;
use Jamesmills\LaravelNotificationRateLimit\ShouldRateLimit;
class NotifyUserOfOrderUpdateNotification extends Notification implements ShouldRateLimit
{
use Queueable;
use RateLimitedNotification;
...
// Opt this notification out of per-channel rate limiting
protected $rateLimitPerChannel = false;
// Five broadcasts per minute; every other channel keeps the configured default.
public function maxAttempts(?string $channel = null): int
{
return $channel === 'broadcast'
? 5
: config('laravel-notification-rate-limit.max_attempts');
}
// One mail per hour, one broadcast per minute, config default for the rest.
public function rateLimitForSeconds(?string $channel = null): int
{
return match ($channel) {
'mail' => 3600,
'broadcast' => 60,
default => config('laravel-notification-rate-limit.rate_limit_seconds'),
};
}
public function rateLimitKey($notification, $notifiable, ?string $channel = null): string
{
// ... your custom key, optionally incorporating $channel ...
}
// Do not log skipped notifications
protected $logSkippedNotifications = false;
public function rateLimitCustomCacheKeyParts()
{
return [
$this->account_id
];
}
class User extends Authenticatable
{
use Notifiable;
protected $fillable = ['id', 'name', 'email', 'groupId'];
public function rateLimitNotifiableKey(): string
{
return $this->group_id;
}
}
class Customer extends Authenticatable
{
use Notifiable;
protected $fillable = ['id', 'name', 'email'];
public function rateLimitNotifiableKey(): string
{
return get_class($this) . '#' . $this->id;
}
}
class Agent extends Authenticatable
{
use Notifiable;
protected $fillable = ['id', 'name', 'email'];
public function rateLimitNotifiableKey(): string
{
return get_class($this) . '#' . $this->id;
}
}