0

I've created a job to send an email at a specific time.

$remind_me_before = "10";

AppointmentReminderJob::dispatchIf(!empty($remind_me_before), $appointment, $user)->delay(Carbon::createFromFormat('Y-m-d H:i', $appointment->start_date)->subMinutes($remind_me_before));

Now what i want is to add the appointment id to the newly created job record, thus i need the job id so i can update it.

i've tried doing the solution of this page :

Laravel 5.8 How to get the job Id?

but no luck.

Edit :

i've found a working solution to do this in this

use Illuminate\Contracts\Bus\Dispatcher;

$job = ( new JOB_CLASS() )->onQueue('QUEUE_NAME')->delay('DELAY');
$id  = app(Dispatcher::class)->dispatch($job);
King
  • 1
  • 1
  • 1
    not sure what you mean by adding the appointment id to the newly created job ... why not just pass the appointment id in the payload? – lagbox Jun 30 '20 at 17:50
  • @lagbox I want to add the appointment id as a separate column in the jobs table, so i can delete the job of the appointment if it got canceled before the job start date. – King Jul 01 '20 at 09:25
  • One approach is to pass the appointment_id in the payload, then when the job runs, check if the appointment is canceled and if so, don't send reminder but return success so the job is complete. That way if the appt is cancelled you don't have to worry about removing jobs. – bumperbox Jul 02 '20 at 10:09
  • @bumperbox Alright ill try your approach – King Jul 03 '20 at 09:49
  • @bumperbox the reason i wanted to add the appointment_id to the jobs table is if a client updated the appointment i want to delete the first job created and make a new one so he doesnt get two mails. not sure how its possible with your approach. – King Jul 03 '20 at 10:24
  • You could have a email sent datetime on the appointment table, and an appointment updated at date time. If the email sent datetime >= appointment updated datetime, don't send email and exit job. That way queued mail jobs will only send mail if something has changed. – bumperbox Jul 03 '20 at 19:59
  • @bumperbox The job uses the SerializesModels trait which serializes the model ID so what i did is fetch the appointment in the handle then do an existence check before sending the mail – King Jul 06 '20 at 09:14

0 Answers0