Laravel中的数组到字符串转换错误

时间:2015-10-17 13:54:27

标签: php laravel queue

我想使用Laravel中的Queue将消息推送到队列。因此,我想首先尝试基本流程,此时会抛出错误。

当我在Laravel中使用CommandBus时,我创建了一个监听器:

听众 - IncidentNotifier.php

<?php

namespace App\Listeners;


use App\Events\Incident\IncidentWasPosted;
use App\Events\EventListener;
use App\Http\Traits\SearchResponder;
use App\Jobs\SendAlarmToResponder;
use Illuminate\Foundation\Bus\DispatchesJobs;

class IncidentNotifier extends EventListener {

    use DispatchesJobs, SearchResponder;

    public function whenIncidentWasPosted(IncidentWasPosted $event) {
        $responders = $this->getResponderInRange($event);
        $this->dispatch(new SendAlarmToResponder($responders));
    }
}

此侦听器应将作业(尚未完成)排队以使用推送通知服务,因为这会在不使用队列的情况下阻止我的系统。

作业 - SendToAlarmResponder.php

<?php

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendAlarmToResponder extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $responders = array();

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

    public function handle($responders)
    {
        var_dump($responders);
    }
}

searchResponder方法

public function getResponderInRange($event) {
    $position[] = array();
    $position['latitude'] = $event->incident->latitude;
    $position['longitude'] = $event->incident->longitude;

    $queryResult = ResponderHelper::searchResponder($position);
    return $queryResult;
}

响应者数组是我想要传递给稍后要处理的作业的变量。这是我从数据库收到的一系列对象,效果很好。但我收到错误消息:

ErrorException in SendAlarmToResponder.php line 19:
Array to string conversion

如何将此阵列移交给作业?

2 个答案:

答案 0 :(得分:5)

这个

$this->$responders = $responders;

应该是:

$this->responders = $responders;

$

后没有->符号

答案 1 :(得分:0)

在您的工作中-SendToAlarmResponder.php

<?php

namespace App\Jobs;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendAlarmToResponder extends Job implements SelfHandling, ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    protected $responders = array();

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

    public function handle($responders)
    {
        var_dump($responders);
    }
}

对此进行更改-

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

至-

public function __construct($responders)
    {
        $this->responders = $responders; // here is the line that you need to change
    }

谢谢。我得到了同样的错误,它可以工作。