dragon8002 2015-08-17 10:57
浏览 406

Laravel 5.1事件ShouldQueue

I have the following listener setup which sends out email notifications to users upon submitting a form. It works perfectly except when I add implements ShouldQueue it returns an error. I am using Iron.io to fire the events.

If I remove implements ShouldQueue it works fine. However it takes longer for the page to load. What effect does implements ShouldQueue have?

<?php

namespace App\Listeners\Timesheet;

use App\Events\Timesheet\TimesheetCreated;
use App\Repos\Email\EmailTemplateRepoInterface;
use App\Repos\User\UserRepoInterface;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail, DbView, Auth;

class EmailTimesheetCreationConfirmation implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(EmailTemplateRepoInterface $template, UserRepoInterface $user)
    {
        $this->template = $template;
        $this->user = $user;
    }

    /**
     * Handle the event.
     *
     * @param  TimesheetCreated  $event
     * @return void
     */
    public function handle(TimesheetCreated $event)
    {
        $timesheet = $event->timesheet;

        $this->dispatchEmail($timesheet->submitted_by, 1); // send submittor email
        $this->dispatchEmail($timesheet->employer_id, 2); // send employer email
        $this->dispatchEmail($timesheet->supervisor_id, 3); // send supervisor email
    }

    /**
     * 
     *
     * @param  TimesheetCreated  $event
     * @return void
     */
    public function dispatchEmail($id, $templateId)
    {
        $user = $this->user->getUserNotificationSettingsById($id);

        if($user)
        {
            if($user->notify_create_timesheet)
            {
                $template = $this->template->findTemplateById(1);

                Mail::queue([], [], function ($message) use ($template, $user) 
                {
                    $message->to($user->email)
                        ->subject($template->subject)
                        ->setBody(DbView::make($template)->with($user->toArray())->render(), 'text/html');
                });
            }
        }
    }
}

The error I get when I use implements ShouldQueue is below:

exception 'ErrorException' with message 'Undefined property: App\Events\Timesheet\TimesheetCreated::$timesheet' in /home/vagrant/Code/App/public/app/Listeners/Timesheet/EmailTimesheetCreationConfirmation.php:33
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 gg加速器加速游戏时,提示不是x86架构
    • ¥15 python按要求编写程序
    • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
    • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件