我在编写给用户发送短信队列的时候,碰到一下问题,请大神给予指点:
用户控制器代码如下:
public function send()
{
$users = User::where('id','<',3)->get();
foreach ($users as $user) {
$this->dispatch(new SendMessageUser($user));
}
return 'Done';
}
队列文件如下:
<?php
namespace App\Jobs;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Overtrue\EasySms\EasySms;
class SendMessageUser implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
/**
* Create a new job instance.
*
* @param User $user
*/
public function __construct(User $user)
{
$this->user = $user;
}
public function handle(EasySms $easySms)
{
/** @var TYPE_NAME $easySms */
$easySms->send($this->user->phone, [
'content' =>"【XXX】尊敬的会员".$this->user->name.",您好,今天是您的生日,祝您生日快乐!"
]);
\Log::info('Send Message to ' . $this->user->phone);
}
}
短信扩展是easy-sms
.env
文件是这样写的:
QUEUE_DRIVER=redis
就会出现这样的错误:
这里的redis
,如果换成database
就完全没有问题,可以发送成功。