繁体   English   中英

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

[英]Array to string conversion error in Laravel

我想使用 Laravel 中的队列将消息推送到队列。 因此,我想先尝试基本流程,但目前会引发错误。

当我在 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

我怎样才能把这个数组交给工作?

这个

$this->$responders = $responders;

应该:

$this->responders = $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);
    }
}

改成这个——

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

到 -

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM