Laravel: Notify User on Job Completion

Viewed 129

I am try to create a notification that alerts the user who ran a job upon completion.

The user does something that creates a job, once that job is complete, notify that user.

What would be the best way of doing this?

1 Answers

You can simply dispatch an event at the and of your job. Edit: When dispatching the job, you can pass the authenticated user:

Job::dispatch(auth()->user());


class Job implements ShouldQueue
{
  protected $user;
  
  public function __construct(User $user)
  {
    $this->user = $user;
  }

  public function handle(User $user)
  {
      // Your logic
      NotifyUser::dispatch($this->user);
  }
}

Or you can make use of job batches (https://laravel.com/docs/master/queues#dispatching-batches) and use the then() callback to dispatch an event

Related