Laravel 中的数组到字符串转换错误
Array to string conversion error in Laravel
我想使用 Laravel 中的 Queue 将消息推送到 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;
}
responders 数组是我想传递给稍后处理的作业的变量。这是我从数据库中收到的一组对象,效果很好。但我收到错误消息:
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
}
谢谢。我得到了同样的错误并且它有效。
我想使用 Laravel 中的 Queue 将消息推送到 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;
}
responders 数组是我想传递给稍后处理的作业的变量。这是我从数据库中收到的一组对象,效果很好。但我收到错误消息:
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
}
谢谢。我得到了同样的错误并且它有效。