I want to create task schedule command. My critieria is:
I have a customer table where customers have due_date
column. So if this column is not null and has date, the date will match current date and send sms to customer mobile number.
scheduleSms Command File:
protected $signature = 'send:scheduleSms';
public function handle()
{
$customer = Customer::where('due_date', '!=', null)->orderBy('id', 'asc')->get();
$today = Carbon::now()->format('d-m-Y');
foreach($customer as $row){
if($row->due_date == $today){
$id = 1;
$sms_settings = SmsSetting::findOrFail($id);
if($sms_settings->schedule_active == 1){
$name = $row->name;
$date = $row->due_date;
$total_due = number_format($row->due);
$msgs = $sms_settings->schedule_sms;
$find = ['#name#','#date#', '#total_due#',];
$replacement = [$name, $date, $total_due ];
$msg = str_replace( $find, $replacement, $msgs);
$send= SMS::shoot($row->mobile, $msg);
}
}
}
}
Kernel.php
protected $commands = [
'App\Console\Commands\scheduleSms'
];
protected function schedule(Schedule $schedule)
{
$schedule->command('send:scheduleSms')
->everyMinute()
->runInBackground()
->timezone('Asia/Dhaka');
}
Suppose Today I scheduled a customer due_date to 2022-08-20, then $send= SMS::shoot($customer->mobile, $msg);
this will execute only once for each customer at 2022-08-20. Also If I have many customers due date, it sends all sms at once which may crush for huge amounts.
Can you please modify the code, so that it takes 2/3second time gap between each sms and send only one sms each customer.
CodePudding user response:
I hope it's useful
Make a command first
php artisan make:command SendSMS
You define code in handle()
function and
$signature = 'sms:send';
(This is example , you can change if you want to be)
In Kernel.php
$schedule->command('sms:send')->daily()->runInBackground();