Reputation: 91
I have a Notification that can have optional attachments. I would like the process to be:
NotificationSent::class
event and check if all notifications have been sent (using $event->notification->batching()
)I have a Notification class called CustomEmail
class CustomEmail extends Notification implements ShouldQueue
{
use Queueable, Batchable;
/* regular notification methods */
}
In EventServiceProvider.php
I'm listening for the NotificationSent::class
and I made a custom DeleteEmailAttachments
handler:
/** EventServiceProvider.php */
protected $listen = [
NotificationSent::class => [
DeleteEmailAttachments::class,
],
];
/** DeleteEmailAttachments.php */
public function handle(NotificationSent $event): void
{
Log::debug('finished sending yet? '.($event->notification->batching() ? 'yes' : 'no'));
}
And finally, this is how I've attempted to dispatch the job:
/** EmailController.php */
// create attachment models (simplified a lot of stuff here but this is the general idea)
$attachments = [];
foreach (request()->file('attachments') as $attachment) {
$attachments[] = App\Models\Attachment::create([
// attachment data here
]);
Storage::disk('local')->put('tempAttachments/'.$attachment->getClientOriginalName(), $attachment, 'private');
}
// create Collection of users to email
$recipients = User::whereIn('id', $request->userIds)->get();
// dispatch Notification in a batch
Bus::batch([
Notification::send($recipients, new CustomEmail($request->body, $request->subject, $attachments)),
])->dispatch();
If I add the ->dispatch()
method (as shown above), it gives me an error
Call to a member function withBatchId() on null
If I remove ->dispatch()
, the emails get sent, but the batch never gets created (no new rows in the job_batches
table). And $event->notification->batching()
always returns false.
Upvotes: 1
Views: 385
Reputation: 1
My solution is to first create a job that sends the notification and dispatch the job.
Suppose you created a job SendEmailJob
, then you can do the following:
Bus::batch([
new SendEmailJob($parameters)
])->dispatch();
Upvotes: 0