簡體   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