skip to Main Content

After the Excel output, I want to send an SMS to the person’s mobile number with the date entered in Excel 5 days before that date.

I also used the job command.

Controller.php

public function import(Request $request)
{
    Excel::import(new OtherImport, $request->file('file')->store('temp'));
    $insuranceData = Other::query()->select('personal_insurance', 'mobile')->get();

    $sentMobileNumbers = [];

    foreach ($insuranceData as $data) {
        $insuranceDate = $data->personal_insurance;
        $mobileNumber = $data->mobile;

        if (!empty($insuranceDate) && !in_array($mobileNumber, $sentMobileNumbers)) {
            try {
                $jalaliDate = Jalalian::fromFormat('Y/m/d', $insuranceDate);
                $carbonDate = Carbon::create($jalaliDate->getYear(), $jalaliDate->getMonth(), $jalaliDate->getDay());
                $smsDate = $carbonDate->subDay(5); 

                if ($smsDate->isPast()) {
                    SendSmsJob::dispatch([$mobileNumber]); 
                    $sentMobileNumbers[] = $mobileNumber; 
                }
            } catch (Exception $e) {
                dd($e->getMessage());
            }
        }
    }
    return back();
} 

2

Answers


  1. A solution could be to send a job with a delay to the queue. Then use the smsDate variable. I would suggest also setting a time to trigger the sending of the sms at a convenient time for the customer.

    Login or Signup to reply.
  2. you need to use

    dispatch()->delay(now()->addDays(5));
    

    Bur remember that you need to do some changes

    1. Change QUEUE_CONNECTION=sync in your ENV file. You have to use a driver that allows you to use the queue, for example ‘database’
    2. Then enable the queue worker with php artisan queue:work or supervisor if you’re in production.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search