2015-10-17 33 views
1

我想使用Laravel中的Queue將消息推送到Queue。因此,我想先嚐試一下基本流程,此時會出現錯誤。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 

如何將此數組移交給作業?

回答

4

$this->$responders = $responders; 

應該是:

$this->responders = $responders; 

沒有$符號後->

+0

天哪......我完全沒看到這一點。現在就像一個魅力!謝謝你,u_mulder – sesc360