【问题标题】:Array to string conversion error in LaravelLaravel中的数组到字符串转换错误
【发布时间】:2015-10-17 13:54:27
【问题描述】:

我想使用 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

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

【问题讨论】:

    标签: php laravel queue


    【解决方案1】:

    这个

    $this->$responders = $responders;
    

    应该是:

    $this->responders = $responders;
    

    -&gt; 之后没有$ 签名

    【讨论】:

    • 天哪......我完全没有看到这个。现在就像一个魅力!谢谢你u_mulder
    【解决方案2】:

    在你的工作中 - 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
        }
    

    谢谢。我得到了同样的错误并且它有效。

    【讨论】:

      猜你喜欢
      • 2019-05-01
      • 2022-01-25
      • 2016-09-01
      • 1970-01-01
      • 2014-12-14
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多