//调用的方法
public function userAdd(){
$data['username']='张三';
$data['password']='123456';
event(new SendEmailCodeEvent($data));
$this->dispatch(new SendMySelfJob());
}
//先去生成一个队列的job 文件例如 php artisan make:job SendMailJob
//邮件发送的队列
<?php
namespace AppJobs;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateMailMessage;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesMail;
class SendMySelfJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Mail::raw('邮件发送',function (Message $message){
$message->to('*******@qq.com');
});
}
}
//监听者
<?php
namespace AppListeners;
use AppEventsSendEmailCodeEvent;
use AppModelsmodelsexamUser;
use IlluminateContractsQueueShouldQueue;
use IlluminateQueueInteractsWithQueue;
class SendEmailCodeListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param SendEmailCodeEvent $event
* @return void
*/
public function handle(SendEmailCodeEvent $event)
{
User::create($event->data);
}
}
//发生的事件者
<?php
namespace AppEvents;
use AppJobsSendMySelfJob;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
class SendEmailCodeEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Get the channels the event should broadcast on.
*
* @return IlluminateBroadcastingChannel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
然后调用就可以了
队列的任务要手动去执行