//调用的方法
public function userAdd(){
$data['username']='张三';
$data['password']='123456';
event(new SendEmailCodeEvent($data));
$this->dispatch(new SendMySelfJob());
}
//先去生成一个队列的job 文件例如 php artisan make:job SendMailJob
//邮件发送的队列
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Mail\Message;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
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 App\Listeners;
use App\Events\SendEmailCodeEvent;
use App\Models\models\exam\User;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
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 App\Events;
use App\Jobs\SendMySelfJob;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
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 \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
然后调用就可以了
队列的任务要手动去执行