如何在laravel 5.2中使用队列事件?

时间:2016-02-23 09:50:20

标签: events queue listener laravel-5.2

我正在使用事件来删除特定信息。 我有 DeleteCourseConfirmation 事件监听器和 DeleteBranchCourse 事件。

工作正常。

以下是 DeleteBranchCourse 事件

的代码
class DeleteBranchCourse extends Event{
use SerializesModels;

private $fee;
private $feeId;
public function __construct($fee,$feeId)
{
    $this->fee=$fee;
    $this->feeId=$feeId;

}

/**
 * Get the channels the event should be broadcast on.
 *
 * @return array
 */
public function broadcastOn()
{
    return [];
}
public function deleteCourse()
{
     $this->fee->destroy($this->feeId);
}}

以下是 DeleteCourseConfirmation 事件监听器

的代码
class DeleteCourseConfirmation{

public function __construct()
{

}

public function handle(DeleteBranchCourse $event)
{

    $event->deleteCourse();
}}

但是当我在 DeleteCourseConfirmation 中实施 ShouldQueue 界面后,尝试 php artisan queue:listen 来排队事件监听器

class DeleteCourseConfirmation implements ShouldQueue{
use InteractsWithQueue;
public function __construct()
{

}

public function handle(DeleteBranchCourse $event)
{

    $event->deleteCourse();
}}

发生错误。

  

模型[App \ Modules \ Branch \ Models \ Fee]

没有查询结果

我正在关注Laravel 5.2文档排队事件监听器

1 个答案:

答案 0 :(得分:0)

找到了解决方案。

首先我改变了事件消防代码

Event::fire(new DeleteBranchCourse($fee,$feeId));

要  Event::fire(new DeleteBranchCourse($feeId));

因为我不需要将Fee model的对象发送到DeleteBranchCourse的构造函数。

我将DeleteBranchCourse类更改为此

class DeleteBranchCourse extends Event{
use SerializesModels;

private $feeId;
public function __construct($feeId)
{
    $this->feeId=$feeId;
}

/**
 * Get the channels the event should be broadcast on.
 *
 * @return array
 */
public function broadcastOn()
{
    return [];
}
public function deleteCourse()
{
     Fee::destroy($this->feeId);
}}

最后从终端

运行此命令
php artisan queue:listen

或将侦听器类的句柄功能更改为此

public function handle(DeleteBranchCourse $event)
{

    $this->attempts($event->deleteCourse());
}}