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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33187432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:31:51  来源:igfitidea点击:

Array to string conversion error in Laravel

phplaravelqueue

提问by sesc360

I want to use the Queue in Laravel to push messages to a Queue. Therefore I wanted to try out the basic flow first, which throws errors at the moment.

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

As I am using the CommandBus in Laravel, I created A listener:

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

Listener - IncidentNotifier.php

监听器 - 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));
    }
}

This listener should queue a job (not yet done) to use the push notification service as this would block my system at the moment without using Queues.

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

Job - SendToAlarmResponder.php

工作 - 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 method

searchResponder 方法

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

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

The responders array is the variable I would like to pass over to the job to be handled there later on. This is an array of objects I received from my Database and works well. But I get the error message:

响应者数组是我想传递给稍后在那里处理的工作的变量。这是我从我的数据库收到的一组对象并且运行良好。但我收到错误消息:

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

How can I hand over this array to the job?

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

回答by u_mulder

This

这个

$this->$responders = $responders;

should be:

应该:

$this->responders = $responders;

with no $sign after ->

$之后没有任何迹象->

回答by Y. Joy Ch. Singha

In your Job - SendToAlarmResponder.php

在您的工作中 - 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);
    }
}

Change it this -

改成这个——

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

to --

到 -

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

Thanks. Same error i got and it work.

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