https://github.com/64robots/batch-notifications
https://github.com/64robots/batch-notifications
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/64robots/batch-notifications
- Owner: 64robots
- Created: 2018-09-04T23:13:30.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-12-10T00:42:29.000Z (over 1 year ago)
- Last Synced: 2025-10-08T23:58:03.141Z (8 months ago)
- Language: PHP
- Size: 98.6 KB
- Stars: 14
- Watchers: 3
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Batch Notifications
## Description
Batch Notifications is a Laravel package that groups repetitive notifications in batches.
This package is intended for cases where notifications are dispatched repeatedly for a same Notifiable model.
So, instead of sending lots of notifications (ex.: email messages) repeatedly to the Notifiable model, the
notifications are grouped in batches that will be sent in periods of time.
## Installation
#### 1 - Require the package
``
composer require 64robots/batch-notifications
``
#### 2 - Publish
``
php artisan vendor:publish --provider="R64\BatchNotifications\BatchNotificationsServiceProvider"
``
#### 3 - Run the migration that was just published
``
php artisan migrate
``
## Usage
#### 1 - Create you notification as you normally do, but add the "$eventables" parameter to the constructor:
```
class DocumentAssignedEmail extends Notification implements ShouldQueue
{
use Queueable;
/** @var Collection $eventables */
private $eventables;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(Collection $eventables)
{
$this->eventables = $eventables;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
if ($this->eventables->count() == 1) {
return (new MailMessage)->view('mail.documents-assigned', [
'user' => $notifiable,
'document' => $this->eventables->first(),
])->subject("New Document Assigned");
}
return (new MailMessage)->view('mail.documents-assigned', [
'user' => $notifiable,
'documents' => $this->eventables,
])->subject("New Documents Assigned");
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
```
#### 2 - Create the notification event for the intended notification:
```
BatchNotificationEvent::queue(
Auth::user(), /* The notifiable model instance. This can be any Notifiable Eloquent model */
$document, /* The eventable instance you want to attach to the notification. All batch's eventables will be present on your notification constructor. This can be any Eloquent model. */
DocumentAssignedEmail::class, /* The fully qualified name of your notification class */
now()->addMinutes(2) /* The minimum interval that the notifiable model will be notified. In this example, the notifications will be sent every two minutes */
);
```
#### 3 - Add the command to your Console/Kernel.php file:
```
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('batch-notifications:dispatch')->everyMinute();
}
//...
}
```
#### 4 - "mail.documents-assigned" view example:
```
@extends('layouts.email')
@section('content')
Hello {{ $user->first_name }},
@if (isset($document))
A new document has been assigned to you: Document #{{ $document->id }}.
@else
New documents have been assigned to you:
@foreach ($documents as $document)
Document #{{ $document->id }}
@endforeach
@endif
@endsection
```