简体   繁体   中英

Array to string conversion error in Laravel

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.

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

Listener - 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

<?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

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?

This

$this->$responders = $responders;

should be:

$this->responders = $responders;

with no $ sign after ->

In your Job - 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM