【问题标题】:How to use queue an event in laravel 5.2?如何在 laravel 5.2 中使用队列事件?
【发布时间】:2016-06-05 02:03:36
【问题描述】:

我正在使用事件来删除特定信息。 我有 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 文档 Queued Event Listeners

【问题讨论】:

  • 你的活动火了怎么办?请提供代码
  • Event::fire(new DeleteBranchCourse($fee,$feeId));
  • 队列数据存储在作业表中,但尝试了 0 次。
  • 当我运行 php artisan queue:listen 时,它会尝试但得到 No query results for model [App\Modules\Branch\Models\Fee] 错误

标签: events queue listener laravel-5.2


【解决方案1】:

找到了解决办法。

首先我更改了事件触发代码

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

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

因为我不需要将费用模型的对象发送到 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());
}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2017-11-18
    • 2017-06-10
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多